Another case of strtr function
<? php tutorial echo strtr ("i loves you", "love", "lovea");?>
The result is
i loves you
Note that looking at the third argument, a, does not appear in the result
I do not recommend using strtr less change
ok, since this strtr function is very troublesome Why use it?
The reason is that it is said to be very fast, strtr four times faster than str_replace
5.php string replacement can use strtr function must use
How to use that comfortable?
This is its second case
strtr (string, array)
6.strtr in line with the intended use
<? php $ table_change = array ('you' => 'her sister'); echo strtr ("i love you", $ table_change);?>
The result is
i love her sister
7. Tips: You think PHP string replacement what you replace the array plus what
such as
<? 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
str_replace
replace
The meaning of replacement is to change a portion of a string to make it a new string to meet new needs. php is usually replaced by str_replace ("content to be replaced", "string to replace the original content", "original string").
echo str_replace ("iwind", "kiki", "i love iwind, iwind said"); // will print "i love kiki, kiki said"
That is, replace all "iwind" in the original string with "kiki".
str_replace is case-insensitive, so you can not imagine replacing "iwind" in the original string with str_replace ("iwind", "kiki", ...).
Str_replace can also achieve many-to-many, many-to-many replacement, but can not achieve one-to-many replacement:
echo str_replace (array ("iwind", "kiki"), "people", "i love kiki, iwind said");
Will be output
i love people, people said
The first parameter in the array ("iwind", "kiki") are replaced by "people"
echo str_replace (array ("iwind", "kiki"), array ("gentle man", "ladies"), "i love kiki, iwind said");
I love ladies, gentle man said. In other words, the elements in the first array are replaced by the corresponding elements in the second array. If there is one array less than the other array elements, the deficiencies will be treated as null.
Somewhat similar to this is strtr,
In addition, php also provides substr_replace, to replace part of the string. Grammar is as follows:
substr_replace (original string, string to replace, position to start replacing [, length of substitution])
Among them, the starting position to replace the beginning of 0, it should be less than the length of the original string. The length to be replaced is optional.
echo substr_replace ("abcdefgh", "def", 3); // will output "abcdef"
echo substr_replace ("abcdefgh", "def", 3, 2); // will output "abcdeffgh"
In the first example, replacing "defgh" with "def" starts with the third position (ie, "d")
In the second example, we also start from the third position (ie, "d"), but we can only replace 2 lengths, ie to e, so we replace "de" with "def".
PHP also provides preg_replace, preg_replace_callback, ereg_replace, eregi_replace and other functions to use regular expressions to complete the string replacement, the usage, please refer to the manual.
Definition and usage
The str_replace () function replaces other characters in a string with one string.
grammar
str_replace (find, replace, string, count) Parameter Description
find necessary. Specify the value to find.
replace Required. Specifies the value to replace the value in find.
string Required. Specifies the string to be searched.
count optional. A variable that counts the number of substitutions.
Tips and Notes Note: This function is case sensitive. Use str_ireplace () to perform a case-insensitive search.
Note: This function is binary safe.
Example 1
<? php
echo str_replace ("world", "john", "hello world!");
?>