Page 1 of 1

regEx headaches...

Posted: Wed Apr 16, 2003 9:33 am
by patrikG
I am writing a template-class (yeah I know there are 100.000 out there, but that's not what I want) and am making use of regular expressions.
After some initial successes, I've hit a wall. A solid granite wall.

I want to have conditional clauses like IF in there and ended up using the HTML-comment tag as a delimiter "<!--" and "-->" (I tried curly brackets with exclamation mark but nothing worked).

Code: Select all

&lt;!--IF relatedNews relatedNews="&lt;TR&gt;
&lt;TD class='red'&gt;&lt;IMG src='../graphix/blank.gif' width='5' height='1' alt='' border='0'&gt;
&lt;/TD&gt;
&lt;TD class='relatedNews' bgcolor='#FFFFFF' valign='top' nowrap&gt;&lt;STRONG&gt;In the News&lt;/STRONG&gt;
&lt;/TD&gt;
&lt;/TR&gt;"--&gt;
My regEx looks like this:

Code: Select all

<?php	preg_match_all("/\<!--IF.?([\w])(="[.*])?"--\>/",$temp,$matches, PREG_PATTERN_ORDER);
?>
Anyone know how I can get the regEx to parse the above code correctly?

Posted: Wed Apr 16, 2003 11:20 am
by patrikG
Nevermind, found the solution. (And am so bloody proud of myself that I am bragging about it by posting the regEx :P )

Code: Select all

<?php
	preg_match_all("/<!-- IF ([\w]+(=")?(.+))("-->)/sU",$temp,$matches, PREG_PATTERN_ORDER);
?>

Posted: Wed Apr 16, 2003 9:06 pm
by McGruff
If regex is giving you headaches relax: everything is working as it should.

Regexing was invented by evil madmen for this exact purpose.