I´m trying to replace some word in a string using preg_replace(). I need to check if the word is not followed by any of this set [a-zA-Z0-9_$.]. In order to achieve this, I'm using negative lookahead, so, for example, if the target word is "foo" and I have a string like:
"hello_foo this foo is an example.foo"
... and I want to replace foo with bar using this regex
$old = "hello_foo this foo is an example.foo";
$new = preg_replace("/(?![a-zA-Z0-9_$.])foo/", "bar" $old);
...this should return
"hello_foo this bar is an example.foo"
which is not working as expected. But, if I remove the a-z set from the regex, it does work, but now it would replace words like this "aaafoo" to "aaabar".
Am I missing something here? I'm new to regex.