not sure what a CSS parser is...
thanks for the the preg_quote suggestion, I thought it worked but even with that I still had the issue of 'mydiv' matching both '.mydiv' and '#mydiv'
This morning I came up with this solution which appears to work... I am sure it is not the most correct but it appears to work so I was wondering if you could review it and let me know what you think:
function tbs_css_find($definition,$property) {
global $css;
$definition_encoded = PostFriendlyCSS_encode($definition);
echo $definition;
if ( substr($definition, 0, 1) == '.' ) {
$css = str_replace($definition,$definition_encoded,$css);
$definition_property_value = preg_replace('/(.*'.$definition_encoded.'.*?\s'.$property.':)([^;]*)(.*)/is','$2',$css);
} else if ( substr($definition, 0, 1) == '#' ) {
$css = str_replace($definition,$definition_encoded,$css);
$definition_property_value = preg_replace('/(.*'.$definition_encoded.'.*?\s'.$property.':)([^;]*)(.*)/is','$2',$css);
} else if ( preg_match('/^\b'.$definition_encoded.'\b/im',$css) ) {
$definition_property_value = preg_replace('/(.*^\b'.$definition_encoded.'\b.*?\s'.$property.':)([^;]*)(.*)/ism','$2',$css);
} else {
$definition_property_value = preg_replace('/(.*'.$definition.'.*?\s'.$property.':)([^;]*)(.*)/is','$2',$css);
}
return trim($definition_property_value);
}
function PostFriendlyCSS_encode($definition) {
$definition = str_replace('#','%%%pound%%%',$definition);
$definition = str_replace('.','%%%dot%%%',$definition);
$definition = str_replace(',','%%%comma%%%',$definition);
$definition = str_replace(':','%%%semicolon%%%',$definition);
$definition = str_replace('=','%%%equal%%%',$definition);
$definition = str_replace('"','%%%quote%%%',$definition);
$definition = str_replace('[','%%%lbracket%%%',$definition);
$definition = str_replace(']','%%%rbracket%%%',$definition);
$definition = str_replace(' ','%%%space%%%',$definition);
return $definition;
}
To make sure I was only matching 'mydiv' and not '.mydiv' and '#mydiv' I added the \b word-boundry to the preg_replace and the multi-line 'm' modifier AND I am matching for a definition at the beginning of a new line.
I am going with the assumption that every new CSS definition is at the beginning of a new line, as it is in every or most CSS files I've ever seen... so I guess this will break if there is a CSS definition that is not at the beginning of a line but at this point I can't think of a better solution...
of course I am open to suggestions...