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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog