replace help needed for BBCode

I can't quite figure out the right pattern to use with PHP's preg_replace function. Here is an example of some BBCode on a phpBB forum.

[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]

I'd like to remove the opening and closing font tags, any number of them, that might be embedded in the text, but keep the text in between. In this case, I would like to end up with:

[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]

The name of the fonts vary, some have spaces in the names, some don't. The pattern match would have to work regardless.

Thanks in advance!


If you are simply removing them, you don't need a too complicated regex...

$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);

Outputs

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]"

See it on ideone.

链接地址: http://www.djcxy.com/p/29870.html

上一篇: PHP BBCode解析器

下一篇: 替换BBCode所需的帮助