I have to replace all HTML markup in a text with BB Codes, and I am struggling with replacing the inline CSS styled elements.
HTML:
<span style="font-family: arial;">Arial</span>
<span style="font-size: 7px;">small text</span>
<span style="color: skyblue;">Blue Text</span>
<div align="center">centered text</div>
Should get replaced by the following:
BB Code:
[FONT=arial]Arial[/FONT]
[SIZE=7]small text[/SIZE]
[COLOR=skyblue]Blue Text[/COLOR]
[CENTER]centered text[/CENTER]
whereas the font families, color codes, and size values can differ.
Non working starting point:
$text = preg_replace('#<span style="color: (.*?);">(.*?)</span>#siU', '[color=$1]$2[/color]', $text);
How about
I just replaced the
#with/and it’s working for me.As for the other replacements:
UPDATE
Fiddled it: phpfiddle.org/main/code/zqu-bgs
(end escaped the
/of the closing tags)