RegEx problems ([quote] tags)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

tony montana
Forum Newbie
Posts: 21
Joined: Tue Sep 28, 2004 11:10 am

RegEx problems ([quote] tags)

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

Post by feyd »

try switching the order you are running them. ([quote=] first)
tony montana
Forum Newbie
Posts: 21
Joined: Tue Sep 28, 2004 11:10 am

Post by tony montana »

Nah didnt work.

I need to somehow impliment it to read

[ quote ] or [ quote = var ]

Actually, no, this wont work :S
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I've used [php_man]preg_match_all[/php_man] to calculate the nesting orders..
tony montana
Forum Newbie
Posts: 21
Joined: Tue Sep 28, 2004 11:10 am

Post by tony montana »

I get 0 for all O_o
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

how about this

Code: Select all

preg_match_all('#(\[quote(=(["'']?)(.*?)\\3)?\]|\[/quote\])#is',$text,$matches,PREG_OFFSET_CAPTURE)
Last edited by feyd on Sat Feb 11, 2006 4:54 pm, edited 1 time in total.
tony montana
Forum Newbie
Posts: 21
Joined: Tue Sep 28, 2004 11:10 am

Post by tony montana »

Ok still 0, but why am I doing that anyway?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

so you can correctly determine the nesting order to do your regular expression run.
tony montana
Forum Newbie
Posts: 21
Joined: Tue Sep 28, 2004 11:10 am

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

Post by feyd »

preg_match* only checks for matches.. I wouldn't expect it to actually do any major processing ;)
tony montana
Forum Newbie
Posts: 21
Joined: Tue Sep 28, 2004 11:10 am

Post by tony montana »

So what can I use instead O_o
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
tony montana
Forum Newbie
Posts: 21
Joined: Tue Sep 28, 2004 11:10 am

Post by tony montana »

Well thats the thing, I cant determine an order because with two types of quote tags theres so many combinations.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I certainly did when I wrote a handler for nesting.. you could also use the outer going in with str_replace.
tony montana
Forum Newbie
Posts: 21
Joined: Tue Sep 28, 2004 11:10 am

Post by tony montana »

Nah, thats why Im doing all this, users could just do [ /quote ] and screw up the table.
Post Reply