- preg_replace: documentation ( source)
<?php
$strings = [
"This is a word",
"<-#word#->",
"<-#<-#<-#word#->#->#->",
];
$p = "word";
foreach ($strings as $string) {
$string = preg_replace("/(?<!<-#)($p)(?!#->)/i", '<-#$1#->', $string);
echo $string . PHP_EOL;
}