Jump to content

Preg: checking if match is between quotes


Full-Demon

Recommended Posts

Well.. to do exactly what you want, you can do something like this:

 

$string = preg_replace('/\G(([^\'"]*|([\'"]).*\3)*)foo/Us', '$1bar', $string);

 

That will replace all 'foo's that are not inside single or double quotes with 'bar's. Of course, do remeber, that if you are trying to match strings that contain text, the quotes used in words like "don't" also count.

PCRE does not support \G.

 

To quote the PHP manual:

 

The \G assertion is true only when the current matching position is at the start point of the match, as specified by the offset argument of preg_match(). It differs from \A when the value of offset is non-zero. It is available since PHP 4.3.3.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.