Reputation: 165
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);
Upvotes: 2
Views: 506
Reputation: 9180
How about
$text = preg_replace('/<span style="color: (.*?);">(.*?)<\/span>/siU', '[color=$1]$2[/color]', $text);
I just replaced the #
with /
and it's working for me.
As for the other replacements:
/<span style="font-size: (.*?)px;">(.*?)<\/span>/
[size=$1]$2[/size]
/<span style="font-family: (.*?);">(.*?)<\/span>/
[font=$1]$2[/font]
/<div align="(.*?)">(.*?)<\/div>/
[$1]$2[/$1]
UPDATE
Fiddled it: phpfiddle.org/main/code/zqu-bgs
(end escaped the /
of the closing tags)
Upvotes: 1
Reputation: 305
Try this:
$str1 = '<span style="font-family: arial;">Arial</span>';
$str2 = '<span style="font-size: 7px;">small text</span>';
$str3 = '<span style="color: skyblue;">Blue Text</span>';
$reg_exp= '/<span\s*style=\"\s*(.*?):\s*(.*?)\s*;\s*\">\s*(.*?)\s*<\/span>/';
$reg_exp_rep = '[$1=$2]$3[/$1]';
$text=preg_replace($reg_exp,$reg_exp_rep, $str1);
echo htmlspecialchars($str1).' <=> '.$text.'<br />';
$text=preg_replace($reg_exp,$reg_exp_rep, $str2);
echo htmlspecialchars($str2).' <=> '.$text.'<br />';
$text=preg_replace($reg_exp,$reg_exp_rep, $str3);
echo htmlspecialchars($str3).' <=> '.$text.'<br />';
Result:
<span style="font-family: arial;">Arial</span> <=> [font-family=arial]Arial[/font-family]
<span style="font-size: 7px;">small text</span> <=> [font-size=7px]small text[/font-size]
<span style="color: skyblue;">Blue Text</span> <=> [color=skyblue]Blue Text[/color]
This is for span tags in general. If you want more different tag recognition or something more specific, you have to define it.
--
EDIT: Something more specific, and that you asked for.
unset($str1,$str2,$str3,$str4,$str5,$str6,$str7,$reg_exp);
$str1 = '
<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>';
$reg_exp[0] = '/<span\s*style=\"\s*(.*?):\s*(.*?)\s*;\s*\">\s*(.*?)\s*<\/span>/';
$reg_exp_rep[0] = '[$1=$2]$3[/$1]';
$reg_exp[1] = '/\[font-family(.*?)\/font-family\]/';
$reg_exp_rep[1] = '[FONT$1/FONT]';
$reg_exp[2]= '/\[font-size(.*?)\/font-size\]/';
$reg_exp_rep[2] = '[SIZE$1/SIZE]';
$reg_exp[3] = '/\[color(.*?)\/color\]/';
$reg_exp_rep[3] = '[COLOR$1/COLOR]';
$reg_exp[4] = '/<div\s*align=\"\s*center\s*"\s*\>\s*(.*?)\s*<\/div>/';
$reg_exp_rep[4]= '[CENTER]$1[/CENTER]';
$text=$str1;
foreach ($reg_exp as $ii => $exp) {
$text=preg_replace($exp,$reg_exp_rep[$ii], $text);
}
echo htmlspecialchars($str1).' <=> '.$text.'<br />';
Result:
<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> <=>
[FONT=arial]Arial[/FONT] [SIZE=7px]small text[/SIZE] [COLOR=skyblue]Blue Text[/COLOR] [CENTER]centered text[/CENTER]
Upvotes: 0