0

我正在制作自己的 bbcode 解析器,当我尝试执行递归引用时遇到了问题。

这是我的代码:

 function forumBBCode($str){
$format_search=array(
'#\[quote=(.*?)\](.*?)\[/quote\]#is'
);

$format_replace=array(
'<blockquote class="quotearea"><i><a class="lblackbu" href="./index.php?status=userview&userv=$1">$1</a> wrote :</i><br />$2</blockquote>'
);

$str=preg_replace($format_search, $format_replace, $str);
$str=nl2br($str);
return $str;
}

我必须添加/编辑什么来做递归报价?换句话说,当一个引用在另一个引用中时......

欢呼和 tnx 的帮助

4

2 回答 2

4

这是一个老问题,但无论如何我都会为 ppl 发布我的解决方案 =]

$open = '<blockquote><span class="bold">Quote: </span><br />'; //the next few lines do the parsing for quote blocks. We 
        $close = '</blockquote>';                                      //have to do it outside the normal parsing arrays because that way does not allow nesting.

        preg_match_all ('/\[quote\]/i', $str, $matches);
        $opentags = count($matches['0']);

        preg_match_all ('/\[\/quote\]/i', $str, $matches);
        $closetags = count($matches['0']);

        $unclosed = $opentags - $closetags;
        for ($i = 0; $i < $unclosed; $i++) {
                $str .= '</blockquote>';
        }
//Do Quotes (nested)
        $str = str_replace ('[quote]', $open, $str);
        $str = preg_replace('/\[quote\=(.*?)\]/is','<blockquote class="darkbg"><span class="bold left">Quote: $1</span><br />', $str);
        $str = str_replace ('[/quote]', $close, $str);
return $str;

和平。

于 2011-02-12T05:53:00.597 回答
1

请参阅此处: PHP 手册中的递归模式

这可能也让您感兴趣,尽管它更多的是技术性:为什么递归正则表达式不是正则表达式?

于 2010-07-09T22:44:19.020 回答