The quote in a quote.

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
Vector
Forum Newbie
Posts: 2
Joined: Fri Jan 13, 2006 10:04 pm
Location: Australia
Contact:

The quote in a quote.

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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?"
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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.
Vector
Forum Newbie
Posts: 2
Joined: Fri Jan 13, 2006 10:04 pm
Location: Australia
Contact:

Post 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 :!:
Post Reply