I am trying to use the ereg_replace to replace substrings in a string. I am having problems when there are several different substrings that I want to replace... example:
If I do this:
$string = 'See the Cat';
$newstring = ereg_replace('Cat','<a href=\'cat.php\'>Cat</a>',$string);
echo $newstring;
It works fine, but I have scenarios where
$string = 'See the Cat, Dog and Monkey';
and I want a link to the Dog and Monkey as well.
I came across a thread that did something like this:
$foo = array(
"Cat" => "<a href=\"cat.php\">Cat</a>",
"Dog" => "<a href=\"dog.php\">Dog</a>",
"Monkey" => "<a href=\"monkey.php\">Monkey</a>"
);
$newstring = ereg_replace(array_keys($foo),array_values($foo),$string);
echo $string;
But, I can't get it to work, and it looks like it will only replace one of the items... but, I could be wrong 🙂