2

我正在制作一个网站,用户可以在其中输入一些 BBcode 和我的 owan BBcode

但是当引号内有引号时我遇到了问题

例如 :

[quote] [quote] 正文一 [/quote] 正文二 [/quote]

预期结果:

----------------------------
| |
| ------------------------------------ |
| |文字一 | |
| ------------------------------------ |
| |
|文本二 |
----------------------------

真正的结果:

----------------------------
|[quote]正文一 |
----------------------------
正文二 [/quote]

我的代码:

function bbcode($replace)
{
$bbcode = array(
"'\[center\](.*?)\[/center\]'is" => "<center>\\1</center>",
"'\[left\](.*?)\[/left\]'is" => "<div style='text-align: left;'>\\1</div>",
"'\[right\](.*?)\[/right\]'is" => "<div style='text-align: right;'>\\1</div>",
"'\[pre\](.*?)\[/pre\]'is" => "<pre>\\1</pre>",
"'\[b\](.*?)\[/b\]'is" => "<b>\\1</b>",
"'\[quote\](.*?)\[/quote\]'is" => "<div class='qrap'><small>Quote:</small><div    class='quote'><br><br>\\1</div></div>",
"'\[i\](.*?)\[/i\]'is" => "<i>\\1</i>",
"'\[enter]'is" => "<br>",
"'\[u\](.*?)\[/u\]'is" => "<u>\\1</u>",
"'\[s\](.*?)\[/s\]'is" => "<del>\\1</del>",
"'\[move\](.*?)\[/move\]'is" => "<marquee>\\1</marquee>",
"'\[url\](.*?)\[/url\]'is" => "<a href='\\1' target='_BLANK'>\\1</a>",
"'\[url=(.*?)\](.*?)\[/url\]'is" => "<a href=\"\\1\" target=\"_BLANK\">\\2</a>",
"'\[img\](.*?)\[/img\]'is" => "<img border=\"0\" src=\"\\1\">",
"'\[img=(.*?)\]'" => "<img border=\"0\" src=\"\\1\">",
"'\[email\](.*?)\[/email\]'is" => "<a href='mailto: \\1'>\\1</a>",
"'\[size=(.*?)\](.*?)\[/size\]'is" => "<span style='font-size: \\1;'>\\2</span>",
"'\[font=(.*?)\](.*?)\[/font\]'is" => "<span style='font-family: \\1;'>\\2</span>",
"'\[color=(.*?)\](.*?)\[/color\]'is" => "<span style='color: \\1;'>\\2</span>",
"'\[youtube\](.*?)\[/youtube\]'is" => "<object height='350' width='425'><param name='movie' value='http://www.youtube.com/v/\\1'><embed src='http://www.youtube.com/v/\\1' type='application/x-shockwave-flash' wmode='transparent' height='350' width='425'></object>",
"'\[quote=(.*?)\](.*?)\[/quote\]'is" => "<div class='qrap'><small>Quote:</small><div class='quote'>Originally posted by: <b>\\1</b><br><br>\\2</div></div>",
"'\[spoiler\](.*?)\[/spoiler\]'is" => "<div class='srap'>
                <div><b>Spoiler</b>: <input type='button' onclick='spoiler(this)' value='SHOW' class='sbutton'></div>
                <div>
                    <div class='spoiler' style='display:none;'>\\1</div>
                </div>
            </div>",
"'\[spoiler=(.*?)\](.*?)\[/spoiler\]'is" => "<div class='srap'>
                <div><b>Spoiler</b> for <i>\\1</i>: <input type='button' onclick='spoiler(this)' value='SHOW' class='sbutton'></div>
                <div>
                    <div class='spoiler' style='display:none;'>\\2</div>
                </div>
            </div>",
"'\[quotepost=(.*?);(.*?)\](.*?)\[/quote\]'is" => "<div class='qrap'><small>Quote:</small><div class='quote'>Originally posted by: <b>\\1</b> (<a href=\"viewcomment.php?id=\\2\" target='_BLANK'>Go to Post</a>)<br><br>\\3</div></div>"
);
$replace = preg_replace(array_keys($bbcode), array_values($bbcode), $replace);
return $replace;
}
?>

有人能帮我吗?提前致谢

4

1 回答 1

0

你的引用正则表达式 ('[quote=(. ?)](. ?)[/quote]') 总是会匹配它在开始标记之后找到的第一个 "[/quote]" 标记,因为你正在进行干预通配符匹配 (.*?) "un-greedy" 与 ? 修饰符。

要获得您正在寻找的行为,您可以贪婪地匹配标签之间的文本,然后递归地搜索该文本以查找与这些匹配的其他引号标签。或者,我认为更简单的解决方案是将打开引号匹配和关闭引号匹配分解为数组中的两个不同条目,并分别用打开的 div 和关闭的 div html 片段替换它们。

于 2012-05-04T12:17:17.053 回答