tags:

views:

51

answers:

1

i have a string like this "{text}...{text}...{text}...text..." i need to replace the "text" value in the string but only the ones that are not in currly or square brackets

+4  A: 

You should then use regular expressions :

/(?<!({|\[))text(?!(}|\]]))/

You can use this with preg_replace() :

preg_replace("/(?<!({|\[))text(?!(}|\]]))/", $replace, $string);
Colin Hebert
thanks but it only works on {} not [] :) i would really be greatful if you help me with it. i suck at regular expressions
Yassir
[ and ] need to be escaped when you want to match against them in a regular expression: \[ and \]
Mark Baker
@Yassir updated.
Colin Hebert
@Colin Hebert : thanks anyway you are missing a ) before the last / :)
Yassir
@Yassir, oups, corrected, thanks.
Colin Hebert