strtr is replacing in an unexpected way

84 Views Asked by At

strtr is replacing in an unexpected way. Please take a look at my code.

$stringX = "Do you remember me?";

strtr($stringX, array("you" => "me", "me" => "you"));

Expected output: Do me remember you.

Actual output: Do me reyoumber you.

How do I get the expected output?

1

There are 1 best solutions below

2
On BEST ANSWER

Try this:

$stringX = "Do you remember me?";

strtr($stringX, array("you" => "me", " me" => " you"));

Note the space before me!!!

NOTE: This will work only in this specific case and might produce unexpected output for other strings!