I need to replace each word only once, and vice versa. To do taht, I used a code that does not work, and I can not find an answer to my question.
Input:
hello w1 w2 w12 new1 new12 new2
Expected output:
hello new1 new2 w12 w1 new12 w2
I need words / phrases into the text to replace.
w1 replace with new1
w12 unchanged
w2 replace with new2
new1 replace with w1
new12 unchanged
new2 replace with w2
Of course my text in Persian
My code is:
$string="hello w1 w2 w12 new1 new12 new2";
$fword= array("w1","w2");
$lword= array("new1","new2");
$cnt=0;
$string=str_replace($fword,$lword,$string,$cnt);
$string=str_replace($lword,$fword,$string,$cnt);
echo "<h2>Change in string: $cnt <br> New String: $string </h2>";
But it is wrong
I also use this code:
$string="hello w1 w2 w12 new1 new12 new2";
$fword= array("w1","w2","new1","new2");
$lword= array("new1","new2","w1","w2");
$cnt=0;
$string=str_replace($fword,$lword,$string,$cnt);
echo "<h2>Change in string: $cnt <br> New String: $string </h2>";