this should work for you
$arrWords = array(
"cat" => "dog",
"dog" => "mouse",
"phone" => "book"
);
$txttest = "My catalogue has a cat and a phone";
$stringArr = explode(' ',$txttest);
foreach($stringArr as $k=>$v){
if(array_key_exists($v,$arrWords)){
$stringArr[$k]=$arrWords[$v];
}
}
echo implode(' ',$stringArr);
EDIT
As per the given situation to replace the string
My catalog has a cat and a phone
using the following array to match the keys and replace with the respective values
$arrWords = array(
"cat" => "dog",
"dog" => "mouse",
"phone" => "book"
);
As there are specific values that are matching the keys there is a strong possibility that our replacement string could be substituting each other if we use preg_replace
within a for loop for above array,
possible suspects ("cat" => "dog","dog" => "mouse")
A better alternative, in this case, is preg_replace_callback
with word boundary
\b
for such requirements as there could be special characters too inside the string other than words like \t
i.e TAB
or any other.
so ideally the original solution provided above should not (in fact never) be used and isn't really a solution as it would fail when there are special characters.
What actually should be done is to first create a regular expression which
should use word boundary with a capturing group inside containing
all the words to be matched as alternatives(using OR operator).
So if I convert my above statement into a regular expression keeping in mind the given search&replace array $arrWords
it would be like.
/\b(cat|dog|phone)\b/gi
the g
modifier, in the end, is the global pattern flag
which assures all matches (don't return after the first match) and i
for the case-insensitive match. We can omit g
modifier when using in preg_replace_callback()
function as the 4th parameter of this function will take care of it.
So if I add my regex
to any online regex utility and provide a string like
My catalog has a cat cat cat cat a\tdog and a phone
(notice that using online utility you should remove the \t with actual TAB when writing the string to the regex editors online to be detected as a special character or \t)
it will highlight all the matches found like below
My catalog has a cat
cat
cat
cat
a dog
and a phone
(the spaces after a
are because of the TAB
)
Now they need to be replaced and after replacing the above statement should be converted to
My catalog has a dog dog dog dog a mouse and a book
so now comes the preg_replace_callback()
and the callback function that will do the trick of replacing and also taking care of not swapping/substituting replacement keys.
$r = preg_replace_callback(
"/\b(".implode("|",array_keys($arrWords)).")\b/i",
function($matchingPharse) use ($arrWords) {
return strtolower($arrWords[strtolower($matchingPharse[0])]);
},
$txttest
);
What we are doing here is that in the first parameter we are making the same regex
above by using array_keys()
to extract the words to be matched in the string from the given array ,as preg_replace_callback()
finds the matching words in the string, it passes them to the callable anonymous function
in the second parameter and returns the replacement words for those matches in the replacement array $arrWords
only one thing new is use
keyword, as we need to access the $arrWords
array and return the respective values from the matched keys and the callback function has only one parameter matches
so the easiest way to pass more than one parameters to the callback function is with the 'use' keyword. I got the idea about it from here under section User Contributed Notes.
So the above will output
My catalog has a dog dog dog dog a mouse and a book
and if we need to check that it preserves the case-insensitivity we need to add the i
feed
INPUT
The cat's mouse(named Mouse), cat (named Cat), and dog (named Giraffe) are in this catalog.
OUTPUT
The dog's mouse(named Mouse), dog (named dog), and mouse (named Giraffe) are in this catalog.
Another case provided by senior member was
$arrWords=["cat"=>"dog","dog"=>"mouse","mouse"=>"Mickey"];
INPUT
The cat's mouse(named Mouse), cat (named Cat), and dog (named Giraffe) are in this catalog.
OUTPUT
The dog's mickey(named mickey), dog (named dog), and mouse (named Giraffe) are in this catalog.
I had a discussion with one of the senior members of the community and he guided me about the severity of misinformation in the original answer that could guide others in the wrong direction which neither SO, me or any other member of this community intend to do, so I tried to improve the answer and would request the OP owner to follow this approach if he intended to use my previous answer. And I apologize for providing a poor solution at the start.