Link to home
Start Free TrialLog in
Avatar of IconMan7
IconMan7

asked on

BBcode quoting

Hi

I'm wondering how I can handle the quoting of other users with BBcode (for a forum).  I can get it to work if only one quote is used:

$x = preg_replace('/\[quote=(.*?)\](.*?)\[\/quote\]/i','<span style=\"font-weight:bold;\">$1 wrote:</span><table border=\"0\" width=\"97%\"><tr><td class=\"some_style\">$2</td></tr></table>',$input);

This would work for something like this:

[quote=myname]Some text[/quote]

However, when I start nesting quotes, it goes wrong:

[quote=myname]Some text [quote=othername]Some other text[/quote][/quote]

Obviously, the preg_replace will not be able to distinguish which [/quote] goes with which [quote=], and match the wrong ones.  Does anyone know how to tackle this?

Thanks.
Avatar of keyboard_junkie
keyboard_junkie

Hi

Seeing no one replied, I have one question.

How do you want the inner quotes to be handled?

Example...

All contain in the first quote table, but each has there own table!

example using a quick function I made!

<span style="font-weight:bold;">first_name wrote:</span><table border="0" width="97%"><tr><td class="some_style"><p>a</p><span style="font-weight:bold;">second_name wrote:</span><table border="0" width="97%"><tr><td class="some_style"><p>b</p><span style="font-weight:bold;">thrid_name wrote:</span><table border="0" width="97%"><tr><td class="some_style"><p>c</p></td></tr></table></td></tr></table></td></tr></table>

This was the original string...

      $str = '[quote=first_name]a[quote=second_name]b[quote=thrid_name]c[/quote][/quote][/quote]';


Just tell me how you want it formatted and I will write you a function that will handle millions of nested quotes!


K_J




Avatar of IconMan7

ASKER

Sounds good :)

The inner quotes each indeed need to have their own table, nested within the table of the previous-level quote.  The example you wrote there seems to be exactly on the mark.

P.S.: I've added some points since writing a complete function is better than I expected. ;)
ASKER CERTIFIED SOLUTION
Avatar of Moeflon
Moeflon

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
In addition you could put it in a function, as you mentioned. I guess it won't be too hard to do, but for the sake of completeness, it should look like this:

-----
function quote($_originalinput)
{
  $x = $_originalinput;
  $lastinput = $x;
         
  while (($x = preg_replace('/\[quote=(.*?)\](.*?)\[\/quote\]/i','<span style=\"font-weight:bold;\"><br /><br />$1 wrote:</span><table border=\"0\" width=\"97%\"><tr><td class=\"some_style\">$2</td></tr></table>', $x)) != $lastinput)
  { $lastinput = $x; }
                   
  return $x;
}
-----
Works like a charm.  Thanks!