PHP function CSS2ARRAY

A very usefull function Im using in our new CMS v6 to load the frontend css into an array in the backend. I can then use this array to:

- Generate a css file for use in the editor and page preview
- Extract menu/sub/subsub colours and sizes for custom font image generation for the menus
- And a few other things like that :)

function css2Array($css){
    $cssArray = array();
    preg_match_all('/(.*?){(.*?)}/isx', $css, $matches);
    foreach ($matches[1] as $key=>$dec){
        $attributes= array();
        preg_match_all('/(.*?):(.*?)(;|$)/isx', $matches[2][$key], $_attributes);
        foreach($_attributes[1] as $i=>$j){
            $attributes[trim($j)] = trim(str_replace('"',",str_replace("'",",$_attributes[2][$i])));
        }
        $classes = explode(',', $dec);
        foreach($classes as $class){
            $newclass = explode('.',$class);
            if(isset($newclass[1])){
               $newclass=$newclass[1];
            }else{
               $newclass=$newclass[0];
            }
            $newclass=trim($newclass);
            $cssArray[$newclass] = (isset($cssArray[$newclass])) ? array_merge($cssArray[$newclass], $attributes) : $attributes;
        }
    }
    return $cssArray;
}

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word