I would like to replace each occurence of hello
with bye
in a sentence or paragraph.
$sentence = 'nothello hello hello hello hello hello';
$find = 'hello';
$replace = 'bye';
$str = preg_replace('/(^|[\s])'.$find.'([\s]|$)/', '$1'.$replace.'$2', $sentence);
echo $str;
I want this to echo nothello bye bye bye bye bye
but instead I get nothello bye hello bye hello bye
.
What am I doing wrong?
I can't use \b
because I am using lots of languages.
*Edit
I guess \b
can work if you use the u
flag.