I'm trying to replace words in a string with words from an array. Problem is the result repeated multiple times. I know there is a gotcha for str_replace, but i can't find any solution for this problem.
I tried with str_replace and preg_replace.
My codes:
With str_replace:
$text = 'Amir and Mahdi are friends.';
$text2 = str_replace(array("Amir","Mahdi"), '{Amir|Mahdi}', $text);
Result: {Amir|{Amir|Mahdi}} and {Amir|Mahdi} are friends.
With preg_replace:
$text = 'Amir and Mahdi are friends.';
$text2 = preg_replace(array("/Amir/","/Mahdi/"), '{Amir|Mahdi}', $text);
Result: {Amir|{Amir|Mahdi}} and {Amir|Mahdi} are friends.
I want this result: {Amir|Mahdi} and {Amir|Mahdi} are friends.