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</t d></tr></t able>',$in put);
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.
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=(.*
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.
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. ;)
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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</t d></tr></t able>', $x)) != $lastinput)
{ $lastinput = $x; }
return $x;
}
-----
-----
function quote($_originalinput)
{
$x = $_originalinput;
$lastinput = $x;
while (($x = preg_replace('/\[quote=(.*
{ $lastinput = $x; }
return $x;
}
-----
ASKER
Works like a charm. Thanks!
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;">
This was the original string...
$str = '[quote=first_name]a[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