Page 1 of 2

RegEx problems ([quote] tags)

Posted: Tue Sep 28, 2004 11:12 am
by tony montana
Ok so both my quote tags work, I used str replace before but users could use [ end quote] (end == /) and break the tables.

So Im using preg replace, however when a quote tag of the same kind is within another it works, but if a is in b then it doesn work (vica versa).

a = [ quote ]
b = [ quote = var ]

Regex

Code: Select all

$forumz_post = preg_replace('/(?<!\\\\)\[quote(?::\w+)?\](.*?)\[\/quote(?::\w+)?\]/si',
		"<table cellspacing="1" cellpadding="4" class="table">
		<tr><td class="headbars">Quote</td></tr>
		<tr><td class="cell">\\1</td></tr></table>\n", $forumz_post);					

		$forumz_post = preg_replace('/(?<!\\\\)\[quote(?::\w+)?=(?:"|"|'')?(.*?)["'']?(?:"|"|'')?\](.*?)\[\/quote\]/si',
		"<table cellspacing="1" cellpadding="4" class="table">
		<tr><td class="headbars">Quote (\\1)</td></tr>
		<tr><td class="cell">\\2</td></tr></table>\n", $forumz_post);

You can see tests (specifically the top one - which shows the problem) here ; http://forumz.wuggawoo.co.uk/index/test

Posted: Tue Sep 28, 2004 12:59 pm
by feyd
try switching the order you are running them. ([quote=] first)

Posted: Tue Sep 28, 2004 1:16 pm
by tony montana
Nah didnt work.

I need to somehow impliment it to read

[ quote ] or [ quote = var ]

Actually, no, this wont work :S

Posted: Tue Sep 28, 2004 1:20 pm
by feyd
I've used [php_man]preg_match_all[/php_man] to calculate the nesting orders..

Posted: Tue Sep 28, 2004 1:33 pm
by tony montana
I get 0 for all O_o

Posted: Tue Sep 28, 2004 1:39 pm
by feyd
how about this

Code: Select all

preg_match_all('#(\[quote(=(["'']?)(.*?)\\3)?\]|\[/quote\])#is',$text,$matches,PREG_OFFSET_CAPTURE)

Posted: Tue Sep 28, 2004 3:02 pm
by tony montana
Ok still 0, but why am I doing that anyway?

Posted: Tue Sep 28, 2004 3:05 pm
by feyd
so you can correctly determine the nesting order to do your regular expression run.

Posted: Tue Sep 28, 2004 3:31 pm
by tony montana
Ok http://forumz.wuggawoo.co.uk/index/test

Its saying 8 matches, yet the quote isnt being parsed - but it has to be for it to return 8.

Posted: Tue Sep 28, 2004 3:35 pm
by feyd
preg_match* only checks for matches.. I wouldn't expect it to actually do any major processing ;)

Posted: Tue Sep 28, 2004 3:40 pm
by tony montana
So what can I use instead O_o

Posted: Tue Sep 28, 2004 3:47 pm
by feyd
when I've used preg_match* to handle nesting, I used the matches to determine which order to do the preg_replaces in.. deepest sets first, in order of appearance.

Posted: Tue Sep 28, 2004 3:48 pm
by tony montana
Well thats the thing, I cant determine an order because with two types of quote tags theres so many combinations.

Posted: Tue Sep 28, 2004 4:01 pm
by feyd
I certainly did when I wrote a handler for nesting.. you could also use the outer going in with str_replace.

Posted: Tue Sep 28, 2004 4:07 pm
by tony montana
Nah, thats why Im doing all this, users could just do [ /quote ] and screw up the table.