Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>First difference:</strong></p> <p>An interesting example of a different behaviour between <code>strtr</code> and <code>str_replace</code> is in the comments section of the PHP Manual:</p> <pre><code>&lt;?php $arrFrom = array("1","2","3","B"); $arrTo = array("A","B","C","D"); $word = "ZBB2"; echo str_replace($arrFrom, $arrTo, $word); ?&gt; </code></pre> <ul> <li>I would expect as result: "ZDDB" </li> <li>However, this return: "ZDDD" (Because B = D according to our array)</li> </ul> <p>To make this work, use "strtr" instead:</p> <pre><code>&lt;?php $arr = array("1" =&gt; "A","2" =&gt; "B","3" =&gt; "C","B" =&gt; "D"); $word = "ZBB2"; echo strtr($word,$arr); ?&gt; </code></pre> <ul> <li>This returns: "ZDDB"</li> </ul> <p>This means that <code>str_replace</code> is a more global approach to replacements, while <code>strtr</code> simply translates the chars one by one.</p> <hr> <p><strong>Another difference:</strong></p> <p>Given the following code (taken from <a href="http://www.simplemachines.org/community/index.php?topic=175031.0" rel="noreferrer">PHP String Replacement Speed Comparison</a>):</p> <pre><code>&lt;?php $text = "PHP: Hypertext Preprocessor"; $text_strtr = strtr($text , array("PHP" =&gt; "PHP: Hypertext Preprocessor" , "PHP: Hypertext Preprocessor" =&gt; "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); ?&gt; </code></pre> <p>The resulting lines of text will be:</p> <blockquote> <p>string(3) "PHP"<br> string(27) "PHP: Hypertext Preprocessor"</p> </blockquote> <hr> <p><strong>The main explanation:</strong></p> <p>This happens because:</p> <ul> <li><p><strong>strtr</strong>: it sorts its parameters by length, in descending order, so:</p> <ol> <li>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.</li> <li>because all the chars of the subject text have been replaced, the process ends there. </li> </ol></li> <li><p><strong>str_replace</strong>: it works in the order the keys are defined, so:</p> <ol> <li>it finds the key “PHP” in the subject text and replaces it with: “PHP: Hypertext Preprocessor”, what gives as result: <blockquote> <p>“PHP: Hypertext Preprocessor: Hypertext Preprocessor”.</p> </blockquote></li> <li><p>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: </p> <blockquote> <p>“PHP: Hypertext Preprocessor”. </p> </blockquote></li> <li><p>there are no more keys to look for, so the replacement ends there.</p></li> </ol></li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload