I need to delete some words within a sentence, but I want the word to be deleted to match completely ...
This is my code:
<?php
function Clean ($text) {
$array = array(
'goo' => '',
'guu' => '',
);
return strtr($text, $array);
}
$OldText = "goo guu mygoo myguu gooyou guuyou";
$NewText = Clean($OldText);
echo $NewText;
?>
I want this: mygoo myguu gooyou guuyou
PHP returns: my my you you
Any ideas? Thank you very much in advance!
You can use preg_replace
with word boundaries and you won't get partial matches:
echo preg_replace('/\s*\b(goo|guu)\b\s*/', '', $OldText);
Demo: https://eval.in/731588