Introduction to STRTR string substitution in PHP _php tutorial

Source: Internet
Author: User
This article describes the STRTR string substitution in PHP for a friend who needs to learn a detailed reference to this article.

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

First of all, for the STRTR function, the first way
Let's take a look at the following example:

The code is as follows Copy Code

Echo strtr ("I Love You", "Lo", "Lo");
?>

The result is

1 I Love You

This result reminds us
1.STRTR It is case-sensitive
The replacement of 2.STRTR is very special, you look at the back that you, the middle O is replaced, this is obviously not our intention

Give a special example of the weird php sttr function

The code is as follows Copy Code

Echo strtr ("I Love You", "Love", "" ");
?>

The result is

I Love You

Nothing will change, so STRTR need to be aware of:
3. Can not be replaced with empty, that is, the final argument cannot be an empty string, of course, the space is possible.

Another case of STRTR function again

The code is as follows Copy Code

Echo strtr ("I Loves You", "Love", "Lovea");
?>

The result is

1 I lOvEs You

Notice that a of the third parameter does not appear in the result
4. I do not recommend using STRTR to replace more

OK, since this strtr function is very troublesome why use it?
The reason is that it's fast
STRTR is said to be four times times faster than Str_replace.
So
5. Be sure to use the STRTR function when using

How do you feel comfortable with that?
This is the second case of it.
STRTR (String,array)
6.STRTR Compliant Use method

The code is as follows Copy Code
$table _change = Array (' you ' + = ' her sister ');
Echo strtr ("I Love You", $table _change);
?>

Result is
I Love her sister

7. Tips: You want to replace what you go to the array plus what
Like what

The code is as follows Copy Code

$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

Remind me again that love is not a good thing to write about.

OK, a mess said a pass, in fact, about STRTR most want to say is this behind the use of
Simple and convenient.
It seems that the following usage also ignores the problem of different character lengths.

Above PHP strtr function experiment, PHP5.2 test pass.

The code is as follows Copy Code

This time the output is Baicai instead of Bai123cai, because str ("Pao")<>< p=""><>

Echo strtr ("paocai!", "Pao", "bai123");

?>


Example 2: When the replaced value is less than the replacement target

The following is the referenced content:

The code is as follows Copy Code

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 Code

$Arr =array (' ao ' = ' oa ', ' ai ' = ' ia ');

Echo strtr ("paocai!", $ARR); This time the output is Poacia

?>


The second is str_replace:

The following is the referenced content:

The code is as follows Copy Code

Echo str_replace ("You", "Paocai", "I Love you!"); Will output I love paocai!

?>


For more detailed information, please see: http://www.bkjia.com/phper/18/strtr_str_replace.htm


http://www.bkjia.com/PHPjc/445303.html www.bkjia.com true http://www.bkjia.com/PHPjc/445303.html techarticle This article describes the STRTR string substitution in PHP for a friend who needs to learn a detailed reference to this article. STRTR (string,from,to) or STRTR (String,array) first for strtr function ...

  • 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.