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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀