GePraxa GePraxa - 4 years ago 94
PHP Question

Replace word of a function with array

I need to delete some words within a sentence, but I want the word to be deleted to match completely ...

This is my code:

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!

Answer Source

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);


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download