【问题标题】:preg_replace help needed for BBCodeBBCode 需要 preg_replace 帮助
【发布时间】:2011-06-21 02:16:03
【问题描述】:

我无法确定与 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]

字体名称各不相同,有些名称中有空格,有些则没有。无论如何,模式匹配都必须起作用。

提前致谢!

【问题讨论】:

    标签: php preg-replace bbcode


    【解决方案1】:

    如果您只是删除它们,则不需要太复杂的正则表达式...

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

    See it on ideone.

    【讨论】:

    • 谢谢亚历克斯。会试一试的!
    猜你喜欢
    • 2011-05-09
    • 1970-01-01
    • 1970-01-01
    • 2011-11-12
    • 2012-04-01
    • 2012-08-24
    • 2022-01-04
    • 2017-10-05
    相关资源
    最近更新 更多