0

Hi im using a pretty basic bbcode parser.

could you guys help me with a problem of mine?

but when for example this is written:

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

the output is this:

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

how would i go and fix that? im realllly bad at the whole preg_replace stuff.

this is my parser:

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);

}

sn0ep
  • 3,843
  • 8
  • 39
  • 63
  • 2
    May I suggest that, for the love of God, you [don't use BBCode](http://stackoverflow.com/questions/3788959/regex-to-split-bbcode-into-pieces/3792262#3792262)? – NullUserException Aug 26 '11 at 17:45
  • What are you trying to fix? What is the problem? – afuzzyllama Aug 26 '11 at 17:46
  • possible duplicate of [nested bb codes won't do what i want](http://stackoverflow.com/questions/7198302/nested-bb-codes-wont-do-what-i-want) – mario Aug 26 '11 at 18:05

1 Answers1

0

This could be adapted with a recursive regex:

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

Which will at least ensure that the output divs will not be incorrectly nested. But you would still have to run the regex twice or three times to catch all quote blocks.

Otherwise it would require a rewrite of your code with preg_replace_callback. Which I cannot be bothered to showcase, since this came up a few dozen times already (try the site search!), has been solved before, etc.

mario
  • 144,265
  • 20
  • 237
  • 291