PHP string replace match whole word
multiple word in string replaced by this
$String = 'Team Members are committed to delivering quality service for all buyers and sellers.';
echo $String;
echo "<br>";
$String = preg_replace(array('/\bTeam\b/','/\bfor\b/','/\ball\b/'),array('Our','to','both'),$String);
echo $String;
Result: Our Members are committed to delivering quality service to both buyers and sellers.
You want to use regular expressions. The \b
matches a word boundary.
$text = preg_replace('/\bHello\b/', 'NEW', $text);
If $text
contains UTF-8 text, you'll have to add the Unicode modifier "u", so that non-latin characters are not misinterpreted as word boundaries:
$text = preg_replace('/\bHello\b/u', 'NEW', $text);