Use and difference between string replacement functions strtr () and str_repalce () in php, strtrstr_repalce
First, let's take a look at the two usage of the php string replacement function strtr:
strtr(string,from,to)
Orstrtr(string,array)
First, the first method for strtr functions is as follows:
Let's take a look at the example below:
<?phpecho strtr("I Love you","Lo","lO");?>
The result is: I lOve yOu
This result reminds us:
1. strtr: It is case sensitive.
2. The replacement of strtr is very special. yOu should check that yOu and O in the middle are replaced. This is obviously not our intention.
Here is another special example to show how strange the sttr function of php is.
<?phpecho strtr("I Love you","Love","");?>
The result is: I Love you
Nothing will change, so strtr should note that:
3. cannot be replaced with null, that is, the last parameter cannot be a Null String. Of course, spaces are acceptable.
Another example of strtr function is as follows:
<?phpecho 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. Be sure to use the strtr function.
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);?>
The result is: I Love her sister.
7. Tips: If you want to replace something, add it 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 writing Love is not feasible.
String replacement.
Syntax:string str_replace(string needle, string str, string haystack);
Return Value: String
Function Type: Data Processing
Description:
This function substitutes str into the haystack string and replaces all needle with str.
Replace % body % with black in the following example
<?php$bodytag = str_replace("%body%", "black", "<body text=%body%>");echo $bodytag;?>
Format:
[@ Str_replace ("old content to be replaced", "new character to replace the original content", $ variable name of the content to be replaced)]
[@ Str_replace (array ('old 1', 'old 2', 'old 3'), array ('new 1', 'new 2', 'new 3 '), $ variable name of the content to be replaced)]
[@ Str_replace (array ('old 1', 'old 2', 'old 3'), 'new content', $ variable name of the content to be replaced)]
Instance:
Replace multiple-to-one: to clear all <p> </p> labels in the content field, replace them with null [@str_replace(array('<p>','</p>'), '', $Content)
]
One-to-one replacement: replace all <br> tags in the content field with <p> [@str_replace('<br>', '<p>', $Content)
]
Replace multiple-to-multiple: replace <br> with <br/> In the content field, <p> change @str_replace(array('<br>', '<p>','</p>') ,array('<br />',']
Summary
The above is all the content of this article. I hope the content of this article will be helpful for everyone to learn or use PHP. If you have any questions, please leave a message. Thank you for your support.