STRTR string replacement usage in PHP, STRTR string _php Tutorial

Source: Internet
Author: User
Tags php programming

STRTR string substitution usage in PHP, STRTR string


This example describes the use of STRTR string substitution in PHP. Share to everyone for your reference. The specific analysis is as follows:

STRTR (string,from,to) or STRTR (String,array)

First, for the first method of the STRTR function, let's look at the following example, the code is as follows:
Copy the Code code as follows: <?php
Echo strtr ("I Love You", "Lo", "Lo");
?>
The resulting results are:
1 I Love You
This result reminds us, 1.STRTR it is case-sensitive, 2.STRTR replacement is very special, you look at the back of that you, the middle O is replaced, this is obviously not our intention, and then give a special example, illustrate this PHP sttr function of the strange, the code is as follows:
Copy the Code code as follows: <?php
Echo strtr ("I Love You", "Love", "" ");
?>
The result is:
I Love You
Nothing will change, so STRTR need to note is: Can not be replaced with empty, that is, the final argument cannot be an empty string, of course, the space is possible, again example STRTR function of another case, the code is as follows:
Copy the Code code as follows: <?php
Echo strtr ("I Loves You", "Love", "Lovea");
?>
The running result is
1 I lOvEs You
Notice that a of the third parameter does not appear in the result.

I do not recommend using STRTR to change more, since this strtr function is very troublesome why use it? The reason is that it's fast, it is said, STRTR faster than str_replace four times times, so can use the STRTR function when must use, then how comfortable? This is the second case: Strtr (String,array)

STRTR the intended use method, the code is as follows:
Copy CodeThe code is as follows: <?php
$table _change = Array (' you ' + = ' her sister ');
Echo strtr ("I Love You", $table _change);
?>
The result is:
I Love her sister
Tip: You want to replace what you add to the array, such as the code is as follows:
Copy CodeThe code is as follows: <?php
$table _change = Array (' you ' + = ' her sister ');
$table _change + = Array (' love ' = ' hate ');
Echo strtr ("I Love You", $table _change);
?>
The result is
I hate her sister
Again remind that love is not possible to write a love, a mess to say a pass, in fact, about STRTR most want to say is the use of the back of the simple and convenient, seemingly behind the usage also ignore the character length of different questions, above PHP strtr function experiment, PHP5.2 test passed, The code is as follows:
Copy CodeThe code is as follows: <?php
This time the output is Baicai instead of Bai123cai, because str ("Pao")
Echo strtr ("paocai!", "Pao", "bai123");
?>
Example 2: When the replaced value is less than the target being replaced, the following is the referenced content, the code is as follows:
Copy CodeThe code is as follows: <?php
This time the output is Laocai instead of Lacai, because str ("Pao") >strlen ("La")
Echo strtr ("paocai!", "Pao", "la");
?>
Example 3: Support for array substitution, the following is the referenced content, the code is as follows:
Copy CodeThe code is as follows: <?php
$Arr =array (' ao ' = ' oa ', ' ai ' = ' ia ');
Echo strtr ("paocai!", $ARR); This time the output is Poacia
?>
Next is Str_replace, the following is the quoted content, the code is as follows:
Copy CodeThe code is as follows: <?php
Echo str_replace ("You", "Paocai", "I Love you!"); Will output I love paocai!
?>

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/917679.html www.bkjia.com true http://www.bkjia.com/PHPjc/917679.html techarticle php STRTR String substitution usage explained in detail, STRTR string This article describes the STRTR string substitution usage in PHP. Share to everyone for your reference. The specific analysis is as follows: Strtr (String,...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.