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

发布于 2024-10-26 21:27:24 字数 339 浏览 0 评论 0原文

由于某些原因,我得到以下不正确嵌套的 BBcode

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

我只想删除嵌套的 url 标签。结果应该是: [url=] Hello world [/url]

我有一篇很长的文章,这种情况发生了很多次。对此有何建议?


如何删除嵌套标签在一篇文章中多次出现,如下所示

[url=] Hello [url=] world [/url][/url] [url=] Hello [url=] world [/url][/url] [url=]你好[url=]世界[/url][/url]

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放低过去 2024-11-02 21:27:24

以下经过测试的脚本应该可以解决问题。它使用递归正则表达式和 preg_replace_callback() 的递归应用程序。它将处理任何嵌套级别的 URL 标签,并删除除最外层标签之外的所有标签:

<?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);
?>

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);
?>
韶华倾负 2024-11-02 21:27:24

这可能会起作用:

$string = preg_replace("/(\[url=[^\]]*\].*)\[url=[^\]]*\](.*)\[\/url\](.*\[\/url\])/is", "$1$2$3", $string);

不过,您应该找到问题的根源,而不是试图消除它。

This might work:

$string = preg_replace("/(\[url=[^\]]*\].*)\[url=[^\]]*\](.*)\[\/url\](.*\[\/url\])/is", "$1$2$3", $string);

You should find the root of the problem rather than trying to undo it, though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文