html - Combining duplicate attributes from a tag in PHP -


i need convert string this <span style="font-size: 16px;" style="color: red;">is</span> test. this <span style="font-size: 16px; color: red;">is</span> test.

there's possibility there more 2 matches or there style, class, style, , styles need combined. , won't spans

unfortunately tidy isn't option more over-bearing in it's cleaning project can accommodate.

going dom document route won't work since multiple style attributes isn't valid, gets contents of first one.

i'd preg_replace, getting matches 1 tag proving quite difficult.

if makes things easier, start life nested tags. have preg_replace combines them there , gives output.

i agree comments above best solution prevent situation in first place, answer question: function combine of style attributes in given string. make sure pass single tag @ time. doesn't matter how many other attributes in tag, nor order matter. combine of style attributes first style value, remove other style attributes:

/**  * @param string $str  * @return string  */ function combinestyles($str) {     $found = preg_match_all("/style=\"([^\"]+)\"/", $str, $matches);     if ($found)     {         $combined = 'style="' . implode(';', $matches[1]) . '"';         $patterns = $matches[0];         $replace = array_pad(array($combined), count($matches[0]), '');         $str = str_replace($patterns, $replace, $str);     }     return $str; } 

Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

Nuget pack csproj using nuspec -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -