Recently I came accross a pretty interesting problem through my group, phpexperts where someone needed a solution to replace badwords, but not if that word is a part of another word. For example if you consider the word “some” as bad, then you can replace all “some”, “.some”,”some,” but not “someone”.
Here is a solution I created for that prob
<?
$var = "somesomesome somesome some, some. someone .some ";
$badword = "some";
$var = preg_replace(array("/([\W]+){$badword}([\W]+)/","/([\W]+){$badword}([\W]+)/"),"$1".str_repeat("*",strlen($badword))."$2",$var);
echo $var;
?>