Let's take a look at the two uses of this PHP string substitution function strtr ():
strtr(string,from,to)
Or first strtr(string,array)
of all, for the STRTR function:
Let's take a look at the following examples:
<?php
Echo strtr ("I Love You", "Lo", "Lo");
? >
The result is: I love you
This result reminds us:
1.STRTR it is case-sensitive
2.STRTR replacement is very special, you look at the back that you, the middle O was replaced, this is obviously not our intention.
Let me give you a special example of how this php sttr function is weird.
<?php
Echo strtr ("I Love You", "Love", "");
? >
The result is: I love you
Nothing's going to change, so strtr need to be aware of:
3. Can not be replaced by NULL, that is, the bottom of the parameter can not be an empty string, of course, the space is OK.
Another example of a STRTR function:
<?php
Echo strtr ("I Loves You", "Love", "Lovea");
? >
The result is: I loves You
Note that the A of the third argument does not appear in the result.
4. I do not recommend using STRTR to change more.
OK, since this strtr function is troublesome, why use it?
The reason is that it's fast. It is said that STRTR is four times times faster than Str_replace.
5. When you can use the STRTR function, you must use it.
How do you feel comfortable with that?
This is the second case of it:
6.STRTR usage in accordance with intent
<?php
$table _change = Array (' You ' => ' her sister ');
Echo strtr ("I Love You", $table _change);
? >
The result: I Love her sister
7. Tips: What do you add to an array when you think about replacing something?
Like what:
<?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
Once again, it doesn't work that love is written in love.
Strings replaced.
Grammar:string str_replace(string needle, string str, string haystack);
return value: String
Function type: Data processing
Content Description:
This function replaces the string str into the haystack string and replaces all needle 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 replace", "new character to replace original content", $ substituted variable name)]
[@str_replace (Array (' Old 1 ', ' Old 2 ', ' Old 3 '), Array (' New 1 ', ' New 2 ', ' new 3 '), $ substituted variable name)]
[@str_replace (Array (' Old 1 ', ' Old 2 ', ' Old 3 '), ' new content ', $ substituted variable name)]
Instance:
Multi-pair substitution: To remove all <p></p> tags from the content field and replace them with empty [ @str_replace(array('<p>','</p>'), '', $Content)
]
One-to-one replacement: Want to replace all <br> tags in the Content field with <p> [ @str_replace('<br>', '<p>', $Content)
]
Multi-pair replacement: Want to change the Content field <br> to <br/>, while <p> for @str_replace(array('<br>', '<p>','</p>') array('<br />','
Summarize
The above is the entire content of this article, I hope the content of this article for everyone to learn or use PHP can help, if there is doubt you can message exchange, thank you for the cloud Habitat Community support.