In my code I get a string which have html tags like so:
$string = '<div style="width:100px;">ABC 1234 <span> Test string, testing this string</span></div>';
Now, I removed the style attribute from the said string using preg_replace:
$string = preg_replace('/(<[^>]+) style=".*?"/i', '', $string);
After removing the style tag, I managed to remove the style attribute so the div tag ended up looking like <div>
. The problem, I encountered after doing this is that I now get an excess >
after the closing tag for the span so the string looks like this now:
$string = '<div>ABC 1234 <span> Test string, testing this string</span> > </div>';
My question is, why did I suddenly get an exccess >
? Is there a different regular expression I can use that will get rid of the style attribute without the additional >
appearing? Or is there any way I can get ride of this?
I tried using str_replace twice like so:
$string = str_replace("\n", "", $string);
$string = str_replace(">>", ">", $string);
But that did not work either.
I am not trying to remove the HTML tags, just the style part.