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;
}
Comments