Page 1 of 1

interesting problem with preg_replace()

Posted: Thu Jul 18, 2002 12:01 am
by will
so i'm using preg_replace() to parse a template for XML data. my template is a simple string along the lines of...

Code: Select all

$html_tpl = '
<b>%title%</b><br>
<i>%time%</i><br>
<a href="%url%">%url%</a><br>
<br>';
where the XML tag names are between the %s. my preg_replace call is...

Code: Select all

$html = preg_replace('/\%(.+)\%/ie', "$\\1", $html_tpl);
The problem is that it is seeing

Code: Select all

%">%
(from the url line of the template) and trying to replace that. is there any way to have each percent sign be matched only once?

Posted: Thu Jul 18, 2002 3:51 am
by gnu2php
Does this work any better?

Code: Select all

$html = preg_replace('/\%(&#1111;^\%]+)\%/ie', "$\\1", $html_tpl);

Posted: Thu Jul 18, 2002 5:44 am
by will
surprisingly, yes it does... although i can't honestly say i understand why :-\

thanks.

Posted: Fri Jul 19, 2002 7:34 pm
by gnu2php
Actually, I don't understand either. I just know that it's usually better to do x[^x]+x than x.+x