php引用内部报价bbcode脚本(嵌套报价)

时间:2012-05-04 11:54:35

标签: php nested quote

我正在建立一个网站,用户可以输入一些BBcodes和我的owan BBcode

但是当报价中有引号

时我遇到了问题

例如:

【引用】 [引用] 文字一 [/引用] 文字二 [/报价]

预期结果:

----------------------------
|                          |
| ------------------------ |
| |text one              | |
| ------------------------ |
|                          |
|text two                  |
----------------------------

真实结果:

----------------------------
|[quote]text one           |
----------------------------
text two [/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;
}
?>
有人能帮帮我吗? 提前谢谢

1 个答案:

答案 0 :(得分:0)

您的报价正则表达式(&#39; [quote =(。?)](。?)[/ quote]&#39;)总是与第一个&#34相匹配; [/报价]&#34;标记它在开始标记之后找到,因为你正在进行干预的外卡匹配(。*?)&#34;非贪婪的&#34;与?改性剂。

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