Hi, This is what I have: preg_replace('/\$var([\.|>|<|=| |;|\+|\-|\*|\/|%|\(|\)|(^a-zA-Z0-9_)|]*)/i', 'something', $code); This should replace "$var=" to "something=". It is doing that, but it's also changing "$var123=" to "something123=" How do I make sure that it only replaces if the next character is not an alphabet or number? I also tried: preg_replace('/\$'. $k. '([^a-zA-Z0-9_\x7f-\xff]*)/i', '\$'.$kk.'$1', $c); but that doesn't work either... Please help Thanks
Hi, could not be this enough ?? preg_replace('/\$var=/i','something=',$str); It should replace only 'var' that directly follows '=' As you don't want any number, alphabet (incl. underscore) as a next char, what else could be in variable name? You can do also something like preg_replace('/\$var[^\w=]*=/i','something=',$str); if you want to specify some exceptions: I don't know maybe I misunderstood the question...
Hi, Thanks for the reply, but it's not just "$var" I want to replace. There are so many. Like: $var= this_one; $some + $$some_other_variable; Now see the last one for example. "$some" is different from "$some_other_variable". When I replace "$some", the other one gets replaced as well. There is another replacement for that one. What I want is that next character of match found should not be a "a-zA-Z0-9_". Then the replacement will work correctly I think... Any regular expression to do this? Thanks
Hi, ok, I see now better... In that case you should try to use word boundary for separating $some and $some_other_var Following example should work like - replace all variables specified by $k with $kk : preg_replace('/\$\b'. $k .'\b/i', '$'.$kk, $c); Is this better for your purpose?
That worked like magic!!! Million thanks to you What does \b do here and while reading about regular expressions I also came across \s etc. Where can I read more on these? Thanks again. Green rep points added.