I want to replace two substrings with each other, as
$str = 'WORD1 some text WORD2 and we have WORD1.';
$result = 'WORD2 some text WORD1 and we have WORD2.';
I use preg_replace to match the replacing words,
$result = preg_replace('/\bWORD1(?=[\W.])/i', 'WORD2', $str);
but, how can I change WORD2 to WORD1 (in the original $str)?
The problem is that the replacement should be done at the same time. Otherwise, word1 changed to word2 will be changed to word1 again.
How can I make the replacements at the same time?
strtr($str, ['WORD1'=>'WORD2', 'WORD2'=>'WORD1']);