Ben
Ben

Reputation: 62434

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 examples like

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

But it lower cases everything, not just the tags.

Upvotes: 0

Views: 826

Answers (4)

Paul Norman
Paul Norman

Reputation: 1683

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

Upvotes: 0

ridgerunner
ridgerunner

Reputation: 34395

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

Upvotes: 0

Toto
Toto

Reputation: 91488

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>

Upvotes: 0

Craig Sefton
Craig Sefton

Reputation: 903

Try the following:

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

Upvotes: 1

Related Questions