Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Nov 2004
    Location
    Edinburgh, SCO
    Posts
    113
    Thanks
    0
    Thanked 0 Times in 0 Posts

    preg_replace, match everything but one expression

    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:

    Code:
    ...
    <!-- 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

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Location
    UK
    Posts
    133
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Change your regex to:

    PHP Code:
    <!-- BEGIN " . $sCondition . " -->(.*?)<!-- END " . $sCondition . " --> 
    It's less "greedy"


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •