替换BBCode所需的帮助
我无法弄清楚使用PHP的preg_replace函数的正确模式。 这是一个在phpBB论坛上的BBCode的例子。
[color=black][font=Times New Roman][color=black]Thanks. As discussed last night/this morning, we were able to Cable and provide some small relief. After enough waste water was evacuated, the camera diagnosis found at 30 foot from clean-out, heavy root penetration and possible damage/separation to the line. We don’t want to throw a cost to fix/repair without a second opinion from xxx. We will have solid cost of repair later today of first thin in the morning. [/color][/font][/color]
我想删除可能嵌入文本中的开始和结束字体标签及其中的任意数量,但将文本保留在两者之间。 在这种情况下,我想结束:
[color=black][color=black]Thanks. As discussed last night/this morning, we were able to Cable and provide some small relief. After enough waste water was evacuated, the camera diagnosis found at 30 foot from clean-out, heavy root penetration and possible damage/separation to the line. We don’t want to throw a cost to fix/repair without a second opinion from xxx. We will have solid cost of repair later today of first thin in the morning. [/color][/color]
字体的名称各不相同,有些名称中有空格,有些则没有。 模式匹配无论如何都必须工作。
提前致谢!
如果你只是删除它们,你不需要太复杂的正则表达式......
$str = '[color=black][font=Times New Roman][color=black]Thanks. As discussed last night/this morning, we were able to Cable and provide some small relief. After enough waste water was evacuated, the camera diagnosis found at 30 foot from clean-out, heavy root penetration and possible damage/separation to the line. We don’t want to throw a cost to fix/repair without a second opinion from xxx. We will have solid cost of repair later today of first thin in the morning. [/color][/font][/color]';
$regex = '/[font=.*?]|[/font]/i';
$str = preg_replace($regex, '', $str);
var_dump($str);
输出
string(457) "[color=black][color=black]Thanks. As discussed last night/this morning, we were able to Cable and provide some small relief. After enough waste water was evacuated, the camera diagnosis found at 30 foot from clean-out, heavy root penetration and possible damage/separation to the line. We donÕt want to throw a cost to fix/repair without a second opinion from xxx. We will have solid cost of repair later today of first thin in the morning. [/color][/color]"
在ideone上查看。
链接地址: http://www.djcxy.com/p/29869.html