i’m making a website where users can input some BBcodes and my owan BBcode
but i have a problem when there is a quote inside a quote
for example :
[quote]
[quote]
text one
[/quote]
text two
[/quote]
expected result:
---------------------------- | | | ------------------------ | | |text one | | | ------------------------ | | | |text two | ----------------------------
the real result:
---------------------------- |[quote]text one | ---------------------------- text two [/quote]
my code:
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;
}
?>
can someone help me?
thanks in advance
Your quote regular expression (‘[quote=(.?)](.?)[/quote]’) is always going to match the first “[/quote]” tag it finds after the opening tag because you are making the intervening wild card match (.*?) “un-greedy” with the ? modifier.
To get the behavior you are looking for, you could match the text in between the tags greedily, then recursively search that text for other quote tags an match those. Or, I think a simpler solution would just be to break the open quote match and close quote match into two different entries in your array, and replace them with the open div and close div html snippets respectively.