The function str_replace() accepts an array as its first parameter, but I don’t see any way to store the string for use in the second parameter where the found text is replaced. For instance, if I want to format around the string with brackets or quotation marks.
Here’s an example. Say I was looking to insert quotation marks around the words in $array when they’re spit back out or change the case of the word to from all lowercase to the case set in $array:
$array = array('Over','Dog','Quick');
$string = "The quick brown fox jumps over the lazy dog";
$string = explode(" ",$string);
foreach ($string as $word) {
echo str_replace($array, "???", $word);
echo " ";
}
That will return The ??? brown fox jumps ??? the lazy ???. I realize this might be a job for regex, but I haven’t been able to figure out how to get it to handle both formatting and replacing. I feel like I’m missing something here, but nothing I google even touches this subject.
See it here in action: http://viper-7.com/q7n7uw
So, for example, if you want to put quotation marks around the string, use this:
See here: http://viper-7.com/2afgL5
In reality though, this is a job for
preg_replace:See here: http://viper-7.com/nAlqzX