I am trying to replace occurances of whole words in a string. There are similar questions here at SO like this and this.
Answers to all these questions recommend using regex like this:
$needle = "a";
$haystack = "oh my dear a";
$haystack = preg_replace("/$needle\b/", 'God', $haystack);
echo $haystack;
This works good for whole words - echoes oh my dear God
But if I replace a
with a.
in both needle and haystack, i;e
$needle = "a.";
$haystack = "oh my dear a.";
the output becomes oh my deGod a.
because .
gets evaluated as regex.
I would want a.
to be replaced by God
with or without regex.