Skip to main content

When to use strtr vs str_replace?


I'm having a hard time understanding when strtr would be preferable to str_replace or vice versa. It seems that it's possible to achieve the exact same results using either function, although the order in which 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');



This outputs




YeXY XYring
YeZ Zring
YeXY XYring
YeZ Zring



Aside from syntax, is there any benefit to using one over the other? Any cases where one would not be sufficient to achieve a desired result?


Source: Tips4allCCNA FINAL EXAM

Comments

  1. First difference:

    An interesting example of a different behaviour 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);
    ?>



    I would expect as result: "ZDDB"
    However, this return: "ZDDD"
    (Because B = D according to our array)


    To make this work, use "strtr" instead:

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



    This returns: "ZDDB"


    This means that str_replace is a more global approach to replacements, while strtr simply translates the chars 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);
    echo $text_strtr;
    echo $text_str_replace;
    ?>


    The resulting lines of text will be:


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




    The main explanation:

    This happens because:


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


    it will give "more importance" to the largest one, and then, as the subject text is itself the largest key of the replacement array, it gets translated.
    because all the chars of the subject text have been replaced, the process ends there.

    str_replace: it works in the order the keys are defined, so:


    it finds the key “PHP” in the subject text and replaces it with: “PHP: Hypertext Preprocessor”, what gives as result:

    “PHP: Hypertext Preprocessor: Hypertext Preprocessor”.
    then it finds the next key: “PHP: Hypertext Preprocessor” in the resulting text of the former step, so it gets replaced by "PHP", which gives as result:



    “PHP: Hypertext Preprocessor”.

    there are no more keys to look for, so the replacement ends there.

    ReplyDelete
  2. i think strtr have more flexible and conditional replacement when used with two arguments, for example : if string is 1, replace with a, but if string is 10, replace with b. This tricks could be done only by strtr.

    $string = "1.10.0001";
    echo strtr($string, array("1" => "a", "10" => "b"));


    see : Php Manual Strtr.

    ReplyDelete
  3. It seems that it's possible to achieve the exact same results using either function


    That's not always true and depends on the search and replace data you provide. For example where the two function differ see: Does PHP str_replace have a greater than 13 character limit?


    strtr will not replace in parts of the string that already have been replaced - str_replace will replace inside replaces.
    strtr will start with the longest key first in case you call it with two parameters - str_replace will replace from left to right.
    str_replace can return the number of replacements done - strtr does not offer such a count value.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex