Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6815229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:46:55+00:00 2026-05-26T20:46:55+00:00

I’m having a hard time understanding when strtr would be preferable to str_replace or

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:46:56+00:00Added an answer on May 26, 2026 at 8:46 pm

    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);
    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:

    This happens because:

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

      1. 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.
      2. 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:

      1. 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”.

      2. 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”.

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I would like to run a str_replace or preg_replace which looks for certain words
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.