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 3 of 3
  1. #1
    Regular Coder mlse's Avatar
    Join Date
    Mar 2005
    Posts
    624
    Thanks
    20
    Thanked 19 Times in 18 Posts

    preg_replace problem

    Hi all,

    I want to replace strings matching the pattern {#<whatever>} (where whatever may vary) with the corresponding replacement, but only in unquoted strings. I know how to do this in all strings, but not in only unquoted ones.

    for example:

    PHP Code:
    $string "The \"quick {#colour}\" {#animal} jumps over the '{#adjective} dog'"
    I know how to get preg_replace to replace {#colour}, {#animal} and {#adjective} with replacement strings, BUT what (generic) pattern should I use to make preg_replace ignore {#colour} and {#adjective} and only replace {#animal}?

    I.e. I want to ignore matching patterns in strings-within-the-string.

    Any help much appreciated
    Last edited by mlse; 02-16-2008 at 05:25 PM.

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    what you want to do cannot be done using a regex...you need to write a simple parser to handle quoted strings within strings
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #3
    Regular Coder mlse's Avatar
    Join Date
    Mar 2005
    Posts
    624
    Thanks
    20
    Thanked 19 Times in 18 Posts
    I thought as much! Thanks anyway


  •  

    Posting Permissions

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