Home >Backend Development >PHP Tutorial >Usage and difference between string replacement functions strtr() and str_repalce() in PHP
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: <br>
##
<?php echo strtr("I Love you","Lo","lO"); ?>The result is: I lOve yOu
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",""); ?>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, 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"); ?>The result is: I lOvEs yOu Note that A in the third parameter does not appear in the result.
<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); ?>The result is: I Love her sister
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); ?>The result is: I hate her sister Let me remind you again that writing Love as love will not work. String replacement.
<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: <br>## [@str_replace("Old content to be replaced", "New characters to replace the original content", $Variable name of the replaced content)]
<br> [@str_replace(array('Old1','Old2','Old3'), array('New1','New2','New3'), $The variable name of the replaced content )]
<br> [@str_replace(array('old1','old2','old3'), 'new content', $Variable name of the replaced content)]
Example: Many-to-one replacement: I want to clear all e388a4556c0f65e1904146cc1a846bee94b3e26ee717c64999d7867364b1b4a3 tags in the content field and replace them with Empty [
@str_replace(array('e388a4556c0f65e1904146cc1a846bee','94b3e26ee717c64999d7867364b1b4a3'), '', $Content) ]One-to-one replacement: want to replace the content All 0c6dc11e160d3b678d68754cc175188a tags in the field are replaced with e388a4556c0f65e1904146cc1a846bee [
]More Multiple replacement: I want to replace 0c6dc11e160d3b678d68754cc175188a in the content field with df250b2156c434f3390392d09b1c9563, replace e388a4556c0f65e1904146cc1a846bee with f32b48428a809b51f04d3228cdf461fa, and clear all 94b3e26ee717c64999d7867364b1b4a3[
, array('df250b2156c434f3390392d09b1c9563','f32b48428a809b51f04d3228cdf461fa','' ), $Content)
]The above is the entire content of this article, I hope it will be helpful to everyone's study.
<br>Related recommendations:
PHP method to get audio file duration<br>##PHP Implementing the method of reading xml files
<br>PHP method of restricting IP access_
phpThe 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!