Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 642365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:09:47+00:00 2026-05-13T21:09:47+00:00

Here is an example of what I’d expect: Input: a { background: red; }

  • 0

Here is an example of what I’d expect:

Input:

a {
    background: red;    
}

p {
    background: red;    
}

strong {
    background: red;
    color: green;
}

Output:

strong{color:green;}
a,p,strong{background:red;}

Most optimisers will output something like this:

strong{background:red;color:green;}
a,p{background:red;}

Notice how it hasn’t picked up the fact that strong, although it contains color: green;, also contains background: red; thus it can be grouped with the others?

Any help you could provide would be greatly appreciated

All the best

Iain

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T21:09:47+00:00Added an answer on May 13, 2026 at 9:09 pm

    Okay, so it seems like what I was after either doesn’t exist or is very hard to come by, so I wrote a script that solves my particular problem. I’m pasting it here in the hope that someone sometime might find it useful.

    <?php
    
    $css = file_get_contents('test.css');
    
    //Strip comments and whitespace. Tabs to spaces
    $css = preg_replace("/\s{2,}/e", ' ', $css);
    $css = preg_replace("/\/\*.*?\*\//", '', $css);
    $css = str_replace("\t", " ", $css);
    $css = str_replace(": ", ":", $css);
    $css = str_replace(" }", "}", $css);
    $css = str_replace("{", "{", $css);
    $css = str_replace(";}", "}", $css);
    
    //Break each rule out onto a new line
    $css = preg_replace("/}\s*/", "}\r\n", $css);
    
    //Break @ rules out onto new lines
    $css = preg_replace('/(@.*?;\s*)/', '\0'."\r\n", $css);
    
    
    //Parse CSS Rules
    $parsed = array();
    $css = explode("\r\n", $css);
    foreach($css as $line => $rule){
        if (preg_match('/(.*?)\{(.*?)\}/i', $rule, $regs)) {
            $clean_selectors =  preg_replace('/\s*,\s*/', ',', $regs[1]);
            $clean_selectors =  preg_replace('/,\s*$|\s$/', '', $clean_selectors);
            $parsed[$line]['selectors'] = explode(',', $clean_selectors);
            $parsed[$line]['properties'] = explode(';', $regs[2]);
        } elseif(trim($rule) != '') {
            $parsed[$line] = $rule;
        }   
    }
    
    //Group CSS by property
    $groups =  array();
    foreach($parsed as $line => $css){
        if(is_array($css)){
            foreach($css['properties'] as $pline => $property){
                if(isset($groups[$property])){
                    $groups[$property] = array_merge($groups[$property], $css['selectors']);
                } else {
                    $groups[$property] = $css['selectors'];
                }
    
            }
        } else {
            $groups[$line] = $css;  
        }
    }
    
    //Output CSS sorted by property
    foreach($groups as $property => $selectors){
        if(is_array($selectors)){
            asort($selectors);
            echo implode(",\r\n", $selectors)." {\r\n\t".trim($property).";\r\n}\r\n\r\n";
        } else {
            echo $selectors."\r\n\r\n"; 
        }
    }
    
    ?>
    

    Now, a couple of cavaets.

    1. No, this is not the most beautiful code in the world, it was done quickly to solve one particular problem I was having once and it’s tailored pretty heavily to the CSS I’ve been given to work with. That said, it should be generic enough to work with most CSS you throw at it.

    2. It is the nature of CSS that sometimes the order in which a rule appears is important to the rendering of the final document. It is likely that if you just run all your CSS through this script that your page won’t render as you expect anymore. I’m just using this script to group page-specific css on a web application that I have no layout control over. As each rule applies to a particular element on a particular page, I’m not expecting huge amounts of dodgyness when I group in this way – it’s just going to make the CSS more maintainable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 338k
  • Answers 338k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer int array1 [] = {1,2,3,4,5,100,200,400}; int array2 [] = {2,6,5,7,2,5,10};… May 14, 2026 at 4:17 am
  • Editorial Team
    Editorial Team added an answer I was able to fix this by doing the following:… May 14, 2026 at 4:17 am
  • Editorial Team
    Editorial Team added an answer The OAuth mailing list would be my first guess. I… May 14, 2026 at 4:17 am

Related Questions

Here is an example of what I've got going on: CREATE TABLE Parent (id
Here is an example of what I want to accomplish and how: class MyClass
How is it possible to serialize sub-objects to $_SESSION? Here is an example of
Here is my scenario, I have query that returns a lot of fields. One
What I really want is a class with a generic constructor, and when a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.