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
2010-10-17 20:47:28
thanks but it only works on {} not [] :) i would really be greatful if you help me with it. i suck at regular expressions
Yassir
2010-10-17 21:03:01
[ and ] need to be escaped when you want to match against them in a regular expression: \[ and \]
Mark Baker
2010-10-17 21:11:46
@Yassir updated.
Colin Hebert
2010-10-17 21:22:20
@Colin Hebert : thanks anyway you are missing a ) before the last / :)
Yassir
2010-10-17 21:35:27
@Yassir, oups, corrected, thanks.
Colin Hebert
2010-10-17 21:36:41