douju4594 2011-03-25 21:04 采纳率: 100%
浏览 19
已采纳

如何使用PHP删除不正确的嵌套BBcode标记

For some reasons I got the following improperly nested BBcode

[url=] Hello [url=] world [/url][/url]

I just want to remove the nested url tags. The result should be: [url=] Hello world [/url]

I have a very long article and this happens many times. Any suggestions for this?


How to remove the nested tags happened many times in one article like this

[url=] Hello [url=] world [/url][/url] [url=] Hello [url=] world [/url][/url] [url=] Hello [url=] world [/url][/url]

Thanks!

  • 写回答

2条回答 默认 最新

  • dongyang5716 2011-03-25 22:30
    关注

    The following tested script should do the trick. It uses a recursive regex and a recursive application of preg_replace_callback(). It will handle URL tags to any nested level and strips all but the outermost tags:

    <?php // test.php 20110325_1500
    $re_url = '%# Match outermost [URL=...]...[/URL] (may have nested URL tags
        (\[URL\b[^[\]]*+\])       # $1: opening URL tag.
        (                         # $2: Contents of URL tag.
          (?:                     # Group of contents alternatives.
            (?:(?!\[/?URL\b).)++  # One or more non-"[URL", non-"[/URL"
          | (?R)                  # Or recursively match nested [URL]..[/URL].
          )*+                     # Zero or more contents alternatives.
        )                         # End $2: Contents of URL tag.
        (\[/URL\s*+\])            # $3: Outermost closing [/URL]
        %six';
    function strip_nested_url_tags($text) {
        global $re_url;
        $return = '_handle_url_callback';
        return preg_replace_callback($re_url, $return, $text);
    }
    function _handle_url_callback($matches) {
        global $re_url;
        static $depth = 0;
        $depth++;
        $return = '_handle_url_callback';
        $matches[2] = preg_replace_callback($re_url, $return, $matches[2]);
        if ($matches[2] === NULL)
        { // On error, preg_replace_callback returns NULL.
            exit('Error - Message is too long or too complex.');
        }
        if (--$depth > 0) return $matches[2];
        return $matches[1] . $matches[2] . $matches[3];
    }
    $data = file_get_contents('testdata.html');
    $data = strip_nested_url_tags($data);
    file_put_contents('testdata_out.html', $data);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图