Hi.
I need to change html tags in a group of templates to lowercase (migrating to xhtml). I have mixed some examples I found drifting in the net and came up with:
$tresc = '<HTML><BODY><A HREF="iNDeX.htML"
NAME="wHAtever" taRGET="aS">This SHOULDNT chANGE casE.</A>
<FORM METHOD="POST" ACTION="Index.php" name="Form1">
<SELECT NAME="nnn">
<OPTION VALUE="CaseSensitive">This is Case Sensitive</Option>
</SELECT>
</FORM>
</Body></html>';
$tresc = preg_replace("/(<\/?)(\w+)([^>]*>)/e",
"'\\1'.strtolower('\\2').'\\3'",
$tresc);
$tresc = preg_replace_callback('/(<\/?)(\w+)([^>]*>)/', 'atribs_lower', $tresc);
echo '<pre>'.htmlentities($tresc).'</pre>';
function atribs_lower($matches) {
return preg_replace('/(\w+)=/e',"strtolower('\\1').'='",stripslashes($matches[0]));
}
The first preg_replace replaces tags. The second one finds the same strings once more and replaces attributes (names but not values). stripslashes in the second one is necessary because the first one adds slashes before double quotes (I don't know why).
This solution kind of works, but can this be done somehow better, more effective - without using preg_replace three times?
Thanks for any suggestions.