【问题标题】:nested bb code quotes how to>嵌套 bb 代码引号如何>
【发布时间】:2011-08-27 01:58:16
【问题描述】:

您好,我正在使用一个非常基本的 bbcode 解析器。

你们能帮我解决我的问题吗?

但是当例如这样写时:

[quote=tanab][quote=1][code]a img{
text-decoration: none;
}[/code][/quote][/quote]

输出是这样的:

tanab said:
[quote=1]
a img{
    text-decoration: none;
}
[/quote] 

我将如何去解决这个问题?我真的不擅长整个 preg_replace 的东西。

这是我的解析器:

function bbcode($input){
$input = htmlentities($input);

$search = array(
            '/\[b\](.*?)\[\/b\]/is',
            '/\[i\](.*?)\[\/i\]/is',
            '/\[img\](.*?)\[\/img\]/is',
            '/\[url=(.*?)\](.*?)\[\/url\]/is',
            '/\[code\](.*?)\[\/code\]/is',
            '/\[\*\](.*?)/is',
            '/\\t(.*?)/is',
            '/\[quote=(.*?)\](.*?)\[\/quote\]/is',
);

$replace = array(
            '<b>$1</b>',
            '<i>$1</i>',
            '<img src="$1">',
            '<a href="$1">$2</a>',
            '<div class="code">$1</div>',
            '<ul><li>$1</li></ul>',
            '&nbsp;&nbsp;&nbsp;&nbsp;',
            '<div class="quote"><div class="quote-writer">$1 said:</div><div class="quote-body">$2</div></div>',

);

return preg_replace($search,$replace,$input);

}

【问题讨论】:

标签: php bbcode quote


【解决方案1】:

这个可以使用递归正则表达式进行调整:

 '/\[quote=(.*?)\](((?R)|.*?)+)\[\/quote\]/is'

这至少可以确保输出 div 不会被错误地嵌套。但是您仍然需要运行正则表达式两次或三次才能捕获所有引用块。

否则将需要使用preg_replace_callback 重写您的代码。我懒得展示,因为这已经出现了几十次(尝试站点搜索!),之前已经解决了,等等。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-01
    • 2011-05-18
    • 2023-03-27
    • 2013-09-21
    • 2022-01-27
    • 2011-02-09
    • 2022-01-19
    相关资源
    最近更新 更多