The replacement functions in php mainly include strtr()
and str_repalce()
. Today we will introduce their differences and usage. , let’s first take a look at the two uses of this PHP string replacement function strtr():
strtr(string,from,to)
orstrtr (string,array)
First of all, focus on the first method of strtr function:
Let’s take a look at the following example:
<?php echo strtr("I Love you","Lo","lO"); ?>
The result obtained is: I lOve yOu
This result reminds us:
1. strtr is case-sensitive
2. The replacement of strtr is very special
Please look at the yOu at the back. The O in the middle has been replaced. This is obviously not our intention. Let me give another special example to illustrate the weirdness of this PHP sttr function.
<?php echo strtr("I Love you","Love",""); ?>
The result is: I Love you
Nothing will change, so what strtr needs to pay attention to is:
3. It cannot be replaced by empty, nor That is, the last parameter cannot be an empty string. Of course, spaces are allowed.
Another example of another case of the strtr function:
<?php echo strtr("I Loves you","Love","lOvEA"); ?>
The result is: I lOvEs yOu
Pay attention to the A in the third parameter, in the result did not appear.
4. I don’t recommend using strtr to exchange less for more
ok. Since this strtr function is quite troublesome, why use it? The reason is, it's fast. It is said that strtr is four times faster than str_replace.
5. When you can use the strtr function, you must use it
The second case:
strtr(string,array)
6. strtr meets your wishes Usage method
<?php $table_change = array('you'=>'her sister'); echo strtr("I Love you",$table_change); ?>
The result is: I Love her sister
7. Tips: Just add whatever you want to replace to the array
For example:
<?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 Love written as love will not work.
String replacement
Syntax:
string str_replace(string needle, string str, string haystack);
Return value: String
Function type: Data processing
Content description:
This function substitutes the string str into the haystack string and replaces all needles with str.
The following example replaces %body% with black
<?php $bodytag = str_replace("%body%", "black", "<body text=%body%>"); echo $bodytag; ?>
Format:
[@str_replace("Old content to be replaced" , "New characters to replace the original content", $Variable name of the replaced content)] [@str_replace(array('Old 1','Old 2','Old 3'), array('New 1',' New 2', 'New 3'), $Variable name of the replaced content)] [@str_replace(array('Old 1', 'Old 2', 'Old 3'), 'New content', $Replaced content Variable name)]
Example:
Many-to-one replacement: I want all
in the content field The p> tag is cleared and replaced with empty [@str_replace(array('
','
'), '', $Content)] [@str_replace('
', '
', $Content)]
Multiple pairs Multi-replace: I want to replace
in the content field with
, replace
with
','
'), array('The above content is for reference only!
Recommended tutorial: PHP video tutorial
The above is the detailed content of What is the php replacement function?. For more information, please follow other related articles on the PHP Chinese website!