In practical programming, we usually use the STRTR () function when we want to implement the function of string substitution. Let's take a look at how the PHP string substitution is implemented.
V First look at the two states of this PHP string substitution function strtr ()
STRTR (String,from,to)
or STRTR (String,array)
First of all, for the STRTR function, the first way
Let's look at the following example of PHP string substitution:
- <? PHP
- Echo strtr ("I Love You
"Lo", "Lo");
- ?>
The result is
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
- <? PHP
You "," Love "," ");
- ?>
The result is
I Love You
PHP string substitution does not change anything, 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
- <? PHP
- Echo strtr ("I Loves You", "Love", "Lovea");
- ?>
The result is
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.
5.PHP string substitution can be used when the STRTR function is used.
How do you feel comfortable with that?
This is the second case of it.
STRTR (String,array)
6.STRTR Compliant Use method
- <? PHP
- $ Table_change Array(' You ' =>' she sister ');
- Echo strtr ("I Love You", $table _change);
- ?>
Result is
I Love her sister
7. Tip: You want to replace the PHP string to replace what you go to the array plus what
Like what
- <? PHP
- $ Table_change Array(' You ' =>' she 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.
The above PHP string substitution experiment, PHP5.2 test pass.
http://www.bkjia.com/PHPjc/446240.html www.bkjia.com true http://www.bkjia.com/PHPjc/446240.html techarticle in practical programming, we usually use the STRTR () function when we want to implement the function of string substitution. Let's take a look at how the PHP string substitution is implemented. ...