【问题标题】:Remove bbcode size from post text从帖子文本中删除 bbcode 大小
【发布时间】:2014-11-19 15:03:16
【问题描述】:

请原谅我不擅长正则表达式。

抱歉:示例已更新

我正在使用 PHP 脚本清理 phpBB 论坛中的一些垃圾。 phpBB 使用某种密钥对 bbcode 进行编码,即出现在冒号后的 8 位字符串。这是一个例子:

outer [size=5:24f81ld3]inner text[/size:24f81ld3] outer

我试图去掉大小是一个字符而不是两个字符的地方,所以上面会变成:

outer inner text outer

但是这个:

outer [size=100:24f81ld3]inner text[/size:24f81ld3] outer

不会变成这样:

outer inner text outer

感谢任何帮助!

【问题讨论】:

    标签: php regex bbcode


    【解决方案1】:

    这是代码

    function remove_phpbb_garbage($str)
    {
      return preg_replace('/\[size=[0-9a-z](?::[0-9a-z]+)?\]((?:[^\[]|\[(?!\/size))*)\[\/size(?::[0-9a-z]+)?\]/i', '\1', $str);
    }
    

    以下是测试用例:

    echo remove_phpbb_garbage("outer [size=1]inner text[/size] outer"), "\n";
    echo remove_phpbb_garbage("outer [size=5:24f81ld3]inner text[/size] outer"), "\n";
    echo remove_phpbb_garbage("outer [size=100:24f81ld3]inner text[/size] outer"), "\n";
    echo remove_phpbb_garbage("outer [size=100:24f81ld3]inner text[/size] outer"), "\n";
    echo remove_phpbb_garbage("outer [size=5:24f81ld3]inner text[/size:24f81ld3] outer"), "\n";
    echo remove_phpbb_garbage("outer [size=100:24f81ld3]inner text[/size:24f81ld3] outer"), "\n";
    echo remove_phpbb_garbage("[b:2q9o3yfx][size=2:2q9o3yfx][font=Arial:2q9o3yfx]Please be careful on how to Jail break your Iphone. In case if you are not already did it. There is a lot of specific instructions How to Jail break your phone on the web. Please read up before attempting a Jail break.[/font:2q9o3yfx][/size:2q9o3yfx][/b:2q9o3yfx] _________________ [bg2ads1:2q9o3yfx][/bg2ads1:2q9o3yfx]"), "\n";
    

    这是输出:

    outer inner text outer
    outer inner text outer
    outer [size=100:24f81ld3]inner text[/size] outer
    outer [size=100:24f81ld3]inner text[/size] outer
    outer inner text outer
    outer [size=100:24f81ld3]inner text[/size:24f81ld3] outer
    [b:2q9o3yfx][font=Arial:2q9o3yfx]Please be careful on how to Jail break your Iphone. In case if you are not already did it. There is a lot of specific instructions How to Jail break your phone on the web. Please read up before attempting a Jail break.[/font:2q9o3yfx][/b:2q9o3yfx] _________________ [bg2ads1:2q9o3yfx][/bg2ads1:2q9o3yfx]
    

    重要提示:该函数正确处理嵌套的size标签!


    如果您对正则表达式感兴趣,这个精彩的资源涵盖了它们的所有细节和风格: http://www.regular-expressions.info/

    【讨论】:

    • 这会处理大小只有 1 个字符的情况吗?
    • @Mark,我已经更新了处理 1 字符大小的函数(没有尾随 :123xyz)。请写出究竟是什么不起作用(输入+预期输出)。
    • 抱歉,还是不行。没有替换。
    • 这里是一个例子:[b:2q9o3yfx][size=2:2q9o3yfx][font=Arial:2q9o3yfx]请注意如何越狱你的 Iphone。如果你还没有这样做。网上有很多关于如何越狱破坏手机的具体说明。请在尝试越狱前阅读。[/font:2q9o3yfx][/size:2q9o3yfx][/b:2q9o3yfx] _________________ [bg2ads1:2q9o3yfx][/bg2ads1:2q9o3yfx]
    • 抱歉我的错误示​​例。结束标签在冒号后也有内容,[/size:24f81ld3]
    【解决方案2】:

    下面的正则表达式只有当它的值正好是一个字符时才会匹配大小标签。

    正则表达式:

    \[(size)=[^:]:([^\]\[]*)\]([^\[\]]*)\[\/\1:\2]
    

    替换字符串:

    $3
    

    DEMO

    代码是,

    preg_replace('~\[(size)=[^:]:([^\]\[]*)\]([^\[\]]*)\[\/\1:\2]~', '$3', $str);
    

    【讨论】:

    • return preg_replace('[(size)=[^:]:[^][]*]([^[]]*)[\/\1]', '$2', $ str);给出这个:警告:preg_replace()[function.preg-replace]:分隔符不能是字母数字或反斜杠
    • 您忘记添加 php 分隔符。查看我的更新。
    • 似乎获得了匹配的第一个实例,而不是所有匹配。有没有办法获得所有匹配项?
    猜你喜欢
    • 1970-01-01
    • 2015-10-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-05
    • 1970-01-01
    • 2020-08-30
    • 1970-01-01
    • 2014-01-20
    相关资源
    最近更新 更多