Strtr string replacement in PHP

Source: Internet
Author: User
Tags strlen

Strtr (string, from,)
Or strtr (string, array)

First, the first method for strtr functions
Let's take a look at the example below:

The code is as follows: Copy code

<? Php
Echo strtr ("I Love you", "Lo", "lO ");
?>

The result is:

1 I lOve yOu

This result reminds us
1. strtr: it is case sensitive.
2. The replacement of strtr is very special. yOu should check that yOu and the Middle O are replaced. This is obviously not our intention.

Here is another special example to show how strange this php sttr function is.

The code is as follows: Copy code

<? Php
Echo strtr ("I Love you", "Love ","");
?>

The result is

I Love you

Nothing will change, so strtr should note that:
3. Cannot be replaced with Null, that is, the last parameter cannot be a null string. Of course, spaces are acceptable.

Another example of strtr function

The code is as follows: Copy code

<? Php
Echo strtr ("I Loves you", "Love", "lOvEA ");
?>

The result is

1 I lOvEs yOu

Note that A of the third parameter does not appear in the result.
4. I do not recommend replacing strtr with less

Okay. Why should I use this strtr function?
The reason is that it is fast
It is said that strtr is four times faster than str_replace
So
5. Be sure to use the strtr function.

How can it be used for comfort?
This is the second case.
Strtr (string, array)
6. Use strtr as expected

The code is as follows: Copy code
<? Php
$ Table_change = array ('you' => 'Her sister ');
Echo strtr ("I Love you", $ table_change );
?>

Result:
I Love her sister

7. Tips: If you want to replace something, add it to the array.
For example

The code is as follows: Copy code

<? 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

Remind me again that writing Love is not feasible.

Okay. Let's talk about it in a mess. In fact, what strtr wants to talk about is the subsequent usage.
Simple and convenient.
It seems that the subsequent usage also ignores the problem of different front and back character lengths.

Above php strtr function experiment, PHP5.2 test passed.

 

The code is as follows: Copy code

<? Php

// At this time, baicai is output instead of 2017123cai, because str ("pao") <strlen ("2017123 ")

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

?>

 


Instance 2: When the length of the value to be replaced is smaller than the target to be replaced

Reference content is as follows:

The code is as follows: Copy code

<? Php

// At this time, laocai is output instead of lacai, because str ("pao")> strlen ("la ")
 
Echo strtr ("paocai! "," Pao "," la ");
 
?>

 


Instance 3: Array replacement is supported.

Reference content is as follows:

The code is as follows: Copy code

<? Php

$ Arr = array ('ao' => 'A', 'Ai' => 'A ');

Echo strtr ("paocai! ", $ Arr); // The output is poacia.

?>

 


Followed by str_replace:

Reference content is as follows:

The code is as follows: Copy code

<? Php

Echo str_replace ("you", "paocai", "I love you! "); // Will output I love paocai!

?>


For more details, see: http://www.111cn.net/phper/18/strtr_str_replace.htm

Related Article

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.