...

View Full Version : preg_replace, match everything but one expression



schotte
05-02-2006, 12:43 AM
Hi there,

I've got the following problem:

A template file which contains HTML (obviously) and comments used for conditions of the form:

<!-- BEGIN condition --> ... <!-- END condition -->

With the following regex I have no problem detecting this, however if there are multiple occurances of one condition, the script will only use the "outer most" of the above comments and for example replace everything else within them.
Here's the regex used within a ereg_replace:

<!-- BEGIN " . $sCondition . " -->(.*)<!-- END " . $sCondition . " -->

And here is an example:



...
<!-- BEGIN condition --> ... more html ... <!-- END condition -->
... more html again ...
<!-- BEGIN condition --> ... more html no3 ... <!-- END condtion -->
...


With the above regex, everything from the first BEGIN to the last END will be replaced, however I only want ... more html ... and ... more html no3 ... to be replaced. I tried working with the not operator, as well as ?= and ?!. Without luck. Anyone?

Schotte

Noodles24
05-02-2006, 09:01 AM
Change your regex to:



<!-- BEGIN " . $sCondition . " -->(.*?)<!-- END " . $sCondition . " -->


It's less "greedy"



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum