string – When to use strtr vs str_replace?

Question:

There are times when it is difficult to understand when to use strtr and when to use str_replace . It seems that it is possible to obtain the same result with any of them, although the order in which the substrings are replaced is reversed. For example:

echo strtr('test string', 'st', 'XY')."\n";
echo strtr('test string', array( 's' => 'X', 't' => 'Y', 'st' => 'Z' ))."\n";
echo str_replace(array('s', 't', 'st'), array('X', 'Y', 'Z'), 'test string')."\n";
echo str_replace(array('st', 't', 's'), array('Z', 'Y', 'X'), 'test string');

Delivery as output:

YeXY XYring
YeZ Zring
YeXY XYring
YeZ Zring

Aside from the syntax, are there any benefits to using one or the other? Are there cases where one would not be enough to obtain the desired result?

Original question: When to use strtr vs str_replace?

Answer:

Translation Note: The original question was posed by @andrewtweber and answered by myself on the English StackOverflow site.

First difference:

An interesting example of a differentiated behavior between strtr and str_replace is in the comments section of the PHP manual:

<?php
$arrFrom = array("1","2","3","B");
$arrTo = array("A","B","C","D");
$word = "ZBB2";
echo str_replace($arrFrom, $arrTo, $word);
?>
  • The expected result is: "ZDDB"
  • However, what you get is: "ZDDD" (Because B corresponds to D according to our arrangement)

To make this work as expected, it's best to use strtr :

<?php
$arr = array("1" => "A","2" => "B","3" => "C","B" => "D");
$word = "ZBB2";
echo strtr($word,$arr);
?>
  • What it returns: "ZDDB"

This means that str_replace takes a more global approach to replacements, while strtr simply translates the characters one by one.


Another difference:

Given the following code: (taken from PHP String Replacement Speed ​​Comparison ):

<?php
$text = "PHP: Hypertext Preprocessor";

$text_strtr = strtr($text
    , array("PHP" => "PHP: Hypertext Preprocessor"
        , "PHP: Hypertext Preprocessor" => "PHP"));
$text_str_replace = str_replace(array("PHP", "PHP: Hypertext Preprocessor")
    , array("PHP: Hypertext Preprocessor", "PHP")
    , $text);
var_dump($text_strtr);
var_dump($text_str_replace);
?>

The resulting lines of text will be:

string (3) "PHP"
string (27) "PHP: Hypertext Preprocessor"


The main explanation:

The reasons for this behavior are:

  • strtr : sorts its parameters by length, in descending order, so:

    1. it will give "more importance" to the longest, and then it will be translated since the main text is itself the longest key in the replacement array.
    2. since all the characters in the main text have already been replaced, then the process ends there.
  • str_replace : works in the order in which the braces were defined, therefore:

    1. finds the "PHP" key in the main text and replaces it with: "PHP: Hypertext Preprocessor", which results:

      "PHP: Hypertext Preprocessor: Hypertext Preprocessor".

    2. then it finds the following key: "PHP: Hypertext Preprocessor" in the text resulting from the previous step, so it is replaced by "PHP", resulting in:

      "PHP: Hypertext Preprocessor".

    3. Since there are no more keys to check, the replacement process ends there.
Scroll to Top