Not quite what you are looking for, but this help may be:
Replace xxx with yyy if and only if xxx is surrounded by spaces or if it is at the beginning or end of a line.
<? $name = "xxx"; $text = "xxx my name is xxx en my website xxx is http://xxx.something.com xxx"; $text = preg_replace("%(?<=^| )".$name."(?= |$)%i", "yyy", $text); echo $text."\n";
It would be easier if PHP support supported variable length outside of statements; because then we could use a more accurate (?<!http://[^ ]+)\bxxx\b
source share