【问题标题】:Regex nested forum quotes (BBCode) [duplicate]正则表达式嵌套论坛报价(BBCode)[重复]
【发布时间】:2012-10-07 22:06:30
【问题描述】:

可能重复:
php regex [b] to <b>

我在使用正则表达式时遇到了问题,我是一个绝对的正则表达式菜鸟。我看不出尝试将 HTML 转换回“BBCode”时出了什么问题。

有人可以看看“取消引用”功能并告诉我我正在犯的明显错误吗? (我知道这很明显,因为我总是发现不明显的错误)

注意:我没有使用递归正则表达式,因为我无法理解它,并且已经开始以这种方式整理引号,以便它们嵌套。

<?php
function quote($str){
    $str = preg_replace('@\[(?i)quote=(.*?)\](.*?)@si', '<div class="quote"><div class="quote-title">\\1 wrote:</div><div class="quote-inner">\\2', $str);
    $str = preg_replace('@\[/(?i)quote\]@si', '</div></div>', $str);
    return $str;
}

function unquote($str){
    $str = preg_replace('@\<(?i)div class="quote"\>\<(?i)div class="quote_title"\>(.*?)wrote:\</(?i)div\><(?i)div class="quote-inner"\>(.*?)@si', '[quote=\\1]\\2',  $str);
    $str = preg_replace('@\</(?i)div\></(?i)div\>@si', '[/quote]', $str);
}
?>

这只是一些帮助测试的代码:

<html>
<head>
    <style>
    body {
        font-family: sans-serif;
    }
    .quote {
        background: rgba(51,153,204,0.4)  url(../img/diag_1px.png);
        border: 1px solid rgba(116,116,116,0.36);
        padding: 5px;
    }

    .quote-title, .quote_title {
        font-size: 18px;
        margin: 5px;
    }

    .quote-inner {
        margin: 10px;
    }
    </style>
</head>
<body>
    <?php
    $quote_text = '[quote=VCMG][quote=2xAA]DO RECURSIVE QUOTES WORK?[/quote]I have no idea.[/quote]';
    $quoted = quote($quote_text);
    echo $quoted.'<br><br>'.unquote($quoted); ?>
</body>

提前致谢,山姆。

【问题讨论】:

    标签: php html regex bbcode


    【解决方案1】:

    好吧,您可以先将 php 类设置为 quote-titlequote_title,但要保持一致。

    然后,将return $str; 添加到您的第二个函数中,您应该就差不多了。

    你可以稍微简化你的正则表达式:

    function quote($str){
        $str = preg_replace('@\[quote=(.*)\]@siU', '<div class="quote"><div class="quote-title">\\1 wrote:</div><div class="quote-inner">', $str);
        $str = preg_replace('@\[/quote\]@si', '</div></div>', $str);
        return $str;
    }
    
    function unquote($str){
        $str = preg_replace('@<div class="quote"><div class="quote-title">(.*) wrote:</div><div class="quote-inner">@siU', '[quote=\\1]',  $str);
        $str = preg_replace('@</div></div>@si', '[/quote]', $str);
        return $str;
    }
    

    但要注意不要用不同的调用替换引号的开始和结束标记。如果您碰巧有其他 bbcode 创建 &lt;/div&gt;&lt;/div&gt; 代码,我认为取消引用会产生一些奇怪的行为。

    【讨论】:

    • >_> 该死的回归……我知道这是显而易见的。谢谢你的简化,好多了!
    【解决方案2】:

    就个人而言,我利用了这样一个事实,即生成的 HTML 基本上是:

    <div class="quote">Blah <div class="quote">INCEPTION!</div> More blah</div>
    

    重复运行正则表达式,直到没有更多匹配项:

    do {
        $str = preg_replace( REGEX , REPLACE , $str , -1 , $c);
    } while($c > 0);
    

    另外,将其作为一个正则表达式来简化此操作:

    '(\[quote=(.*?)\](.*?)\[/quote\])is'
    '<div class="quote"><div class="quote-title">$1 wrote:</div><div class="quote-inner">$1</div></div>'
    

    【讨论】:

    • 我认为在某处读到使用问号会“反转量词的贪婪”,这不仅仅是让他们变得懒惰。因此,如果量词是惰性的,因为您使用了 U 选项,那么您会再次使用 .*? 使它们变得贪婪,这不是您想要的。
    • 啊,是的。我以为 U 是“unicode”,但那是小写的u。好的,将编辑。
    猜你喜欢
    • 1970-01-01
    • 2012-11-01
    • 2018-05-30
    • 2011-02-23
    • 1970-01-01
    • 2021-06-28
    • 2010-12-07
    • 2013-07-04
    • 1970-01-01
    相关资源
    最近更新 更多