This article mainly introduces the use of strtr string replacement in PHP, explains in detail the usage and skills of strtr string replacement with a large number of examples, and compares it with str_replace to deepen understanding, for more information, see
This article mainly introduces the use of strtr string replacement in PHP, explains in detail the usage and skills of strtr string replacement with a large number of examples, and compares it with str_replace to deepen understanding, for more information, see
This example describes how to replace strtr strings in PHP. Share it with you for your reference. The specific analysis is as follows:
Strtr (string, from, to) or strtr (string, array)
First, let's look at the following example for the first method of strtr function. The Code is as follows:
The Code is as follows:
<? 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 note that the O in the middle of yOu is replaced. This is obviously not our intention. Let's take another special example, the code for this php sttr function is as follows:
The Code is as follows:
<? Php
Echo strtr ("I Love you", "Love ","");
?>
The result is:
I Love you
Nothing will change, so strtr should note that it cannot be replaced with null, that is, the last parameter cannot be a Null String. Of course, spaces are acceptable, the Code is as follows:
The Code is as follows:
<? Php
Echo strtr ("I Loves you", "Love", "lOvEA ");
?>
The running result is
1 I lOvEs yOu
Note that A of the third parameter does not appear in the result.
I don't recommend using strtr to replace more with less. 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 it must be used when the strtr function is used. How can it be used comfortably? This is its second case: strtr (string, array)
The Code of strtr is as follows:
The Code is as follows:
<? Php
$ Table_change = array ('you' => 'her sister ');
Echo strtr ("I Love you", $ table_change );
?>
Result:
I Love her sister
TIPS: Add the following code to the array if you want to replace it:
The 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
Remind me again that writing Love into love is not feasible. In fact, what strtr wants to talk about is that the subsequent usage is simple and convenient, it seems that the subsequent usage also ignores the problem of different front and back character lengths. The above php strtr function experiment, PHP5.2 passed the test, the Code is as follows:
The Code is as follows:
<? Php
// At this time, the output is baicai instead of 2017123cai, because str ("pao ") Echo strtr ("paocai! "," Pao "," 2017123 ");
?>
Instance 2: when the length of the replaced value is smaller than the target, the following is the reference content. The Code is as follows:
The Code is as follows:
<? Php
// At this time, laocai is output instead of lacai, because str ("pao")> strlen ("la ")
Echo strtr ("paocai! "," Pao "," la ");
?>
Example 3: array replacement is supported. The reference content is as follows:
The Code is as follows:
<? Php
$ Arr = array ('ao' => 'A', 'ai' => 'A ');
Echo strtr ("paocai! ", $ Arr); // The output is poacia.
?>
The second is str_replace. The reference content is as follows:
The Code is as follows:
<? Php
Echo str_replace ("you", "paocai", "I love you! "); // Will output I love paocai!
?>
I hope this article will help you with PHP programming.
,