View Full Version : preg_replace problem

02-16-2008, 04:59 PM
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:

$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 :)

02-16-2008, 09:23 PM
what you want to do cannot be done using a regex...you need to write a simple parser to handle quoted strings within strings

02-18-2008, 10:40 AM
I thought as much! Thanks anyway :)