This example describes the STRTR string substitution usage in PHP. Share to everyone for your reference. The specific analysis is as follows:
STRTR (string,from,to) or STRTR (String,array)
First of all, for the STRTR function, let's look at the following example, the code is as follows:
Copy Code code as follows:
<?php
Echo strtr ("I Love You", "Lo", "Lo");
?>
The results obtained are:
1 I Love You
This result reminds us that 1.strtr it is case-sensitive, 2.STRTR replacement is very special, you look at the back that you, the middle O is replaced, this is obviously not our intention, and then give a special example of this PHP sttr function of the strange, the code is as follows:
Copy Code code as follows:
<?php
Echo strtr ("I Love You", "Love", "");
?>
The result:
I Love You
Nothing is going to change, so strtr need to be aware of: can not be replaced by NULL, that is, the bottom of the argument can not be an empty string, of course, the space is OK, again for example STRTR function of another case, the code is as follows:
Copy Code code as follows:
<?php
Echo strtr ("I Loves You", "Love", "Lovea");
?>
Run result is
1 I Loves You
Note that the A of the third argument does not appear in the result.
I do not recommend using STRTR to change more, since this strtr function is very troublesome why use it? The reason is that it is fast, it is said, strtr than str_replace four times times faster, so can use the STRTR function must use, that how to use just comfortable? This is the second case: Strtr (String,array)
Strtr the use method that conforms to the will, the code is as follows:
Copy Code code as follows:
<?php
$table _change = Array (' You ' => ' her sister ');
Echo strtr ("I Love You", $table _change);
?>
The results are:
I Love her sister
Tip: When you think about replacing something, you add something to the array, such as the following code:
Copy Code code 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
Again remind that love written love is not workable, a mess said a pass, in fact, about STRTR most want to say is that the use of the following is simple and convenient, looks like the use of the following also ignores the character length before and after the different problems, the above PHP strtr function experiment, PHP5.2 test passed, The code is as follows:
Copy Code code as follows:
<?php
This time the output is Baicai rather than Bai123cai, because str ("Pao") <strlen ("bai123")
Echo strtr ("paocai!", "Pao", "bai123");
?>
Example 2: When the value of the replacement is less than the replacement target, the following is the referenced content, the code is as follows:
Copy Code code as follows:
<?php
This time the output is Laocai rather than 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 code as follows:
<?php
$Arr =array (' ao ' => ' oa ', ' ai ' => ' ia ');
Echo strtr ("paocai!", $ARR); This time the output is Poacia
?>
Next is Str_replace, the following is the referenced content, the code is as follows:
Copy Code code as follows:
<?php
Echo str_replace ("You", "Paocai", "I Love you!"); Will output I love paocai!
?>
I hope this article will help you with your PHP programming.