我必须用BB代码替换文本中的所有HTML标记,我正在努力替换内联CSS样式元素。
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>
应该被以下内容取代: BB代码:
[FONT=arial]Arial[/FONT]
[SIZE=7]small text[/SIZE]
[COLOR=skyblue]Blue Text[/COLOR]
[CENTER]centered text[/CENTER]
而字体系列,颜色代码和大小值可能不同。
非工作起点:
$text = preg_replace('#<span style="color: (.*?);">(.*?)</span>#siU', '[color=$1]$2[/color]', $text);
答案 0 :(得分:1)
怎么样
$text = preg_replace('/<span style="color: (.*?);">(.*?)<\/span>/siU', '[color=$1]$2[/color]', $text);
我刚刚将#
替换为/
,这对我有用
至于其他替代品:
/<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]
<强>更新强>
摆弄它:phpfiddle.org/main/code/zqu-bgs
(结束转义结束标记的/
)
答案 1 :(得分:0)
试试这个:
$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 />';
结果:
<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]
通常用于span标签。如果您想要更多不同的标签识别或更具体的内容,您必须对其进行定义。
-
编辑:更具体的,你要求的。
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 />';
结果:
<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]