PHP substitution string function strtr () and Str_replace () comparison

Source: Internet
Author: User
Tags comparison strlen

STRTR (string,from,to):

Replace one character at a distance, whichever is shorter in the From and to, for example:
STRTR ("Aidenliu", "AI", "B") replaces only the a output that appears in the original string: Bidenliu
STRTR ("Aidenliu", "a", "BC") replaces only the a output that appears in the original string: Bidenliu
This function is case sensitive, and if multiple substitutions occur, each replacement's blueprint is the original one, rather than replaced on the basis of the previous substitution, as

The code is as follows Copy Code
STRTR ("Aidenliu", "AA", "a@") will output Aidenliu instead of @idenliu
STRTR (String,array):

Array is an associative array that replaces the corresponding key appearing in the original string with the values in the associative array, and if multiple substitutions occur, each replacement is the original string, rather than replaced on a replacement basis (this point differs from Str_replace).

The code is as follows Copy Code
STRTR (String,from,to) and Strtr (String,array):
STRTR (String,from,to) is a character substitution, while STRTR (String,array) is a string replacement
STRTR ("Aidenliu", "AI", "") do not replace (the second parameter is a zero-length string)
STRTR ("Aidenliu", Array ("AI" => ")" replaced

Refer to the following article:
The substitution functions in PHP are mainly STRTR (), Str_repalce (), but do you know the difference and usage of these two functions?
Let's take a look at the two uses of this PHP string substitution function strtr ():
STRTR (String,from,to)
or STRTR (String,array)
First, the first way for the STRTR function:
Let's take a look at the following examples:

The results obtained are:
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.

The result:
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:

The result:
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:
STRTR (String,array)
6.STRTR usage in accordance with intent

The results are:
I Love her sister
7. Tips: What do you add to an array when you think about replacing something?
Like what:

The result:
I hate her sister
Once again, it doesn't work that love is written in love.
Strings replaced.
Syntax: 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. Mlevine@adtraq.com (11-apr-1999) points out that in the PHP 3.0.7 version, this function has some bugs, and nadeem@bleh.org (05-jun-1999) complements the PHP 3.0.8 version function and returns to normal.
Usage examples
The following example replaces%body% with black

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: Want to put everything in the Content field
The label is removed, replaced with empty [@str_replace ('
','
'), ", $Content)]
One-to-one replacement: Want to put all the content in the field
tag to change
[@str_replace ('
', '
', $Content)]
Multi-pair replacement: Want to put in the Content field
Into
, while
Change
, put
Clear all [@str_replace Array ('
', '
','
'), Array ('
','
', '), $Content)]


comparison of Strtr and Str_replace


First of all, these 2 functions have the function of replacing characters. But the STRTR is 4 times times more than the str_replace performance. For details, see the following decomposition:
The first is the STRTR function:
Example 1: When

The code is as follows Copy Code

<?php
This time the output is Baicai rather than Bai123cai, because str ("Pao") <strlen ("bai123")
Echo strtr ("paocai!", "Pao", "bai123");
?>

Instance 2: When the replaced value is less than the replacement target

The code is as follows Copy Code

<?php
This time the output is Laocai rather than Lacai, because str ("Pao") >strlen ("La") echo strtr ("paocai!", "Pao", "la"); ?>

Example 3: Array substitution support

The code is as follows Copy Code
<?php
$Arr =array (' ao ' => ' oa ', ' ai ' => ' ia ');
Echo strtr ("paocai!", $ARR); This time the output is Poacia
?>

The second is str_replace:

The code is as follows Copy Code
<?php
Echo str_replace ("You", "Paocai", "I Love you!"); Will output I love paocai!
?>

Summary: STRTR He is associated with the length of the character, but Str_replace is not contacted, estimating the length of the string at the time of the operation will be read, so it will be much slower than the STRTR.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.