开发者

How to lowercase only HTML elements

How can I lowercase all HTML elements but not their attributes or attribute values?

I found a lot of ex开发者_高级运维amples like

$newString = preg_replace("/<[^>]+>/e", "strtolower('\\0')", $oldString); 

But it lower cases everything, not just the tags.


Try the following:

$newString = preg_replace( "/<([^> ]+)/e", "strtolower('\\0')", $oldString )


how about:

$oldString = "<H1 CLASS='abc'>HGKJHG</H1>\n< P ClAsS='AbC123'>HKJGHG</P>";
$newString = preg_replace("~(</?\s*\w+)~e", "strtolower('\\1')", $oldString); 
echo $newString,"\n";

output:

<h1 CLASS='abc'>HGKJHG</h1>
< p ClAsS='AbC123'>HKJGHG</p>


$newString = preg_replace("/</?\w+/e", "strtolower('\\0')", $oldString); 


function change_case_tags($string, $action = 'mb_strtolower')
{
    $string = preg_replace('!<([^> ]+)!e', "$action('\\0')", $string);

    if(strpos($string, '=') !== false)
    {
        return $string = preg_replace('!(?:\s([^\s]+)=([\'"])?(.+)\\2)!Uie', "$action(' \\1').'='.str_replace('\\\\\\', '', '\\2').'\\3'.str_replace('\\\\\\', '', '\\2').''", $string);
    }

    return $string;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜