0

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?

jessica
  • 1,667
  • 1
  • 17
  • 35

1 Answers1

0

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!

Szabolcs Páll
  • 1,402
  • 6
  • 25
  • 31
  • 1
    Well, just keep in mind that this solution is very specific and will only work with this case. As soon as another word that starts with 'me' is added, it'll have a similar problem again. – Katai Aug 24 '15 at 07:33
  • 1
    It wouldn't won't if remember is member, but if you do both spaces in front and back, and what if it's me? without a space? – jessica Aug 24 '15 at 07:47