Another case of strtr functions
<? Php Tutorial 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 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
5. Use the strtr function to replace php strings.
How can it be used for comfort?
This is the second case.
Strtr (string, array)
6. Use strtr as expected
<? Php $ table_change = array ('you' => 'Her sister'); echo strtr ("I love you", $ table_change);?>
I love her sister
7. Tips: If you want to replace the php string with the replacement string, add the replacement string 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
Replacement is to change a part of a string to a new character string to meet new requirements. In php, use str_replace ("content to be replaced", "string to replace the original content", and "original string") to replace.
Echo str_replace ("iwind", "kiki", "I love iwind, iwind said"); // output "I love kiki, kiki said"
Replace all "iwind" in the original string with "kiki ".
Str_replace is case sensitive, so you cannot imagine replacing the "iwind" in the original string with str_replace ("iwind", "kiki ".
Str_replace can also replace multiple-to-one and multiple-to-many, but cannot replace one-to-multiple:
Echo str_replace (array ("iwind", "kiki"), "people", "I love kiki, iwind said ");
I love people, people said
Array ("iwind", "kiki") in the first parameter is replaced with "people"
Echo str_replace (array ("iwind", "kiki"), array ("gentle man", "ladies"), "I love kiki, iwind said ");
Output I love ladies and gentle man said. That is to say, the elements in the first array are replaced by the corresponding elements in the second array. If there is an array with fewer elements than the other array, the insufficiency will be treated as null.
Similar to strtr,
In addition, php also provides substr_replace to replace some strings. Syntax:
Substr_replace (original string, string to be replaced, starting position [, length of replacement])
Here, the starting position of replacement starts from 0 and should be smaller than the length of the original string. The length to be replaced is optional.
Echo substr_replace ("abcdefgh", "def", 3); // output "abcdef"
Echo substr_replace ("abcdefgh", "def", 3, 2); // output "abcdeffgh"
In the first example, replace "defgh" with "def" starting from the third position (that is, "d"
In the second example, it is also replaced from the third position (that is, "d"), but only two lengths can be replaced, that is, to e, therefore, replace "de" with "def ".
Php also provides functions such as preg_replace, preg_replace_callback, ereg_replace, and eregi_replace to use regular expressions to replace strings. For usage instructions, see the manual.
Definition and usage
The str_replace () function uses one string to replace other characters in the string.
Str_replace (find, replace, string, count) parameter description
Find is required. Specifies the value to be searched.
Replace is required. Specifies the value to replace the value in find.
String is required. Specifies the string to be searched.
Count is optional. A variable that counts the number of replicas.
Tips and comments
Note: This function is case sensitive. Use str_ireplace () to perform a case-insensitive search.
Note: This function is binary secure.
Echo str_replace ("world", "john", "hello world! ");