kalimero
kalimero

Reputation: 107

preg_replace of "Word" in a sentence and "Word." on the end of a sentence

i want to preg_replace "Word" in PHP.

    $ret = 'I gave my Word to you.';    
    $pattern = '/\bWord\b/i';
    $ret = preg_replace($pattern,"Heart",$ret);
// echo $ret; = "I gave my Heart to you";

This works so far. But if the sentence is "I gave you my Word." or "I gave you my Word!" it doesn't change the "Word." into "Heart."

Upvotes: 1

Views: 14060

Answers (1)

Oliver O'Neill
Oliver O'Neill

Reputation: 1254

Not sure what the problem is. works for me.

<?
    $ret = 'I gave my Word to you Word.';
    $pattern = '/\bWord\b/i';
    $ret = preg_replace($pattern,"Heart",$ret);
    var_dump($ret);
?>

string(29) "I gave my Heart to you Heart."

Upvotes: 5

Related Questions