The replacement functions in PHP mainly include strtr() and str_repalce(). The following article mainly introduces the difference and usage between the two. The article introduces it in detail through example code. Friends in need can refer to it, and follow the editor to learn together.
First, let’s take a look at the two uses of this PHP string replacement function strtr():
strtr(string,from,to)
Or strtr(string,array)
First, focus on the first way of strtr function:
Let’s look at the following example:
##
<?php echo strtr("I Love you","Lo","lO"); ?>
This result reminds us:<br/>
1.strtr is case-sensitive <br/>
2.strtr replacement is very special, please pay attention to the yOu at the back and the O in the middle Replaced, this is obviously not our intention.
Give another special example to illustrate the weirdness of this php sttr function<?php echo strtr("I Love you","Love",""); ?>
3. It cannot be replaced by empty, that is, the last parameter cannot be an empty string, of course, spaces are allowed. of.
Another example of another case of strtr function:<?php echo strtr("I Loves you","Love","lOvEA"); ?>
<br/>
4. I do not recommend using strtr to exchange less for more. <br/>
ok, since this strtr function is quite troublesome, why do you still use it?<br/>
The reason is that it is very fast. It is said that strtr is four times faster than str_replace.<br/>
5. Be sure to use the strtr function when you can. <br/>
How to use it comfortably?<br/>
This is its second case:<br/>
strtr(string,array)
6.strtr Intentional use Method
<?php $table_change = array('you'=>'her sister'); echo strtr("I Love you",$table_change); ?>
7. Tips: Just replace whatever you want What to add to the array<br/>
For example:<?php $table_change = array('you'=>'her sister'); $table_change += array('Love' => 'hate'); echo strtr("I Love you",$table_change); ?>
<br/>
Syntax:string str_replace(string needle, string str, string haystack);<br/>
<br/>
Function type: Data processingContent description:<br/>
This function substitutes the string str into the haystack string and replaces all needles with str.<br/>
The following example replaces %body% with black<br/>
<?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('Old1','Old2','Old3'), array('New1','New2','New3'), $The variable name of the replaced content )]
[@str_replace(array('old1','old2','old3'), 'new content', $Variable name of the replaced content)]
Example: Many-to-one replacement: I want to clear all
tags in the content field and replace them with Empty [ @str_replace(array('','
'), '', $Content) ]One-to-one replacement: want to replace the content All
tags in the field are replaced with
[
@str_replace('', $Content)
]More Multiple replacement: I want to replace
in the content field with
, replace
with
','
'), array('<br />','<hr>','' ), $Content)
]The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
##PHP Implementing the method of reading xml files
PHP method of restricting IP access_
The above is the detailed content of Usage and difference between string replacement functions strtr() and str_repalce() in PHP. For more information, please follow other related articles on the PHP Chinese website!