fork download
  1. <?php
  2.  
  3. $str = 'WORD1 some text WORD2 and we have WORD1.';
  4. echo preg_replace_callback('~\b(?:WORD1|WORD2)\b~', function ($m) {
  5. return $m[0] === "WORD1" ? "WORD2" : "WORD1";
  6. }, $str);
Success #stdin #stdout 0s 82624KB
stdin
Standard input is empty
stdout
WORD2 some text WORD1 and we have WORD2.