Today, we will talk about the strtr () function used to replace PHP Strings. For beginners, The strtr () function of the PHP string replacement function is still relatively simple, and we hope that the content introduced in this article will give you a deep understanding of the specific significance of this function.
- Explanation of the execution time of PHP computing pagesCodeImplementation
- PHP operator category Overview
- Correct use of PHP quotation marks
- Describes the composition of PHP Strings
- Detailed description of the str_replace function of PHP
Let's take a look at the two statuses of the PHP string replacement function strtr ().
Strtr (string, from,)
Or strtr (string, array)
First, the first method for replacing the strtr () function with a PHP string
Let's take a look at the example below:
- <? PHP
- Echo strtr ("I love you", "Lo", "Lo ");
- ?>
The result is:
I love you
This result reminds us
1. strtr: It is case sensitive.
2. The replacement of strtr () in the PHP string replacement function is very special. You should note that you and the O in the middle are replaced later. This is obviously not our intention.
Here is another special example to show how strange this php sttr function is.
- <? 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 replacing strtr () with a PHP string
- <, Balencigag handbag ;? PHP
- Echo strtr ("I loves you", "love", "Lovea ");
- ?>
The result is
I loves you
Note that a of the third parameter does not appear in the result.
4. I do not recommend replacing the strtr () function with a PHP string
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
5. Be sure to use the strtr function.
How can it be used for comfort?
This is the second case.
Strtr (string, array)
6. php string replacement function strtr ()
- <? 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.
- <? 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.
The above PHP string replacement function strtr () experiment, php5.2 test passed.