Page 1 of 1

The quote in a quote.

Posted: Fri Jan 13, 2006 10:19 pm
by Vector
Howdy doody. Simple question, but I don't know how the answer goes...

My current PHP regex is

Code: Select all

'/\[quote=(.*)\](.*?)\[\/quote\]/is'
and that formats to

Code: Select all

"Quote ($1):<br /><br /><div style='margin:2px 20px;border-style:hidden solid;border-width:2px;padding:2px 15px;'>$2</div>"
Or put more simply,

Code: Select all

[quote=bob]Hello everyone![/quote]
Will format to

Code: Select all

Quote (bob) :
    Hello everyone!
So basically it takes the persons name set inside the [quote=MyName] tag, and puts brackets around it, then it indents the main text.

My problem is that when I have two of these such tags embedded in each other, it produces some... er... odd, but expected results. For example:

Code: Select all

This is the main post
[QUOTE=PersonA]
    [QUOTE=PersonB]
        This is the embedded quote
    [/QUOTE]
    This is the first-level quote
[/QUOTE]
This is the main post
I want this to come up with 2 quote blocks, but in actuality it will format to

Code: Select all

This is the main post
Quote (PersonA][QUOTE=PersonB]This is the embedded quote[/QUOTE]) :
    This is the first level quote

This is the main post
As you can see, the "quotee" is not just the name of the person that was specified, but everything up to the end of the embedded quote.

So I guess my question is, how do I get the php regex to search from the outside in, so it will properly process the same tag types embedded?

Thanks

--Vector

<edit> You can view the undesired effect Here

Posted: Sat Jan 14, 2006 12:02 am
by feyd
generally, it involves using a regex to find the openings and closings, because any number of levels can exist in the nesting. This is how phpbb works, and is the most simple I've found using the partial pass style that many boards use.

So basically, use preg_match_all() to find the starts. Stops can be found at the same time.

This is entirely untested

Code: Select all

preg_match_all('#\[quote(?:\=(["\'])(.*?)\\1)?\]|\[/quote\]#i',$text,$matches,PREG_OFFSET_CAPTURE);
Using the captured offsets, you can work out substring replacements for the final, render pass.

Posted: Sat Jan 14, 2006 12:04 pm
by Weirdan
Looks like recursive regexp could be of help here.

There is support for recursive regexps in PCRE, but the question is: "does pcre bundled with php support them?"

Posted: Sat Jan 14, 2006 1:45 pm
by Jenk
viewtopic.php?t=38384

[/shamefully shameless pimp]

a few tweaks to store the content of '/\[quote=([^\[\]])+?\]/i' in the $cClose array and you'll be happy as larry.

Posted: Sat Jan 14, 2006 5:25 pm
by Vector
Thanks all for the fast reply's :)
I'll try some of these ideas and post again if I need any more help.

Thanks again :!: