Difference between string replacement functions strtr () and str_repalce () in PHP

Source: Internet
Author: User

Turn: http://zhangdewen06452.blog.163.com/blog/static/1108031320109229103491/

 

Strtr (string, from, ):

Replace characters one by one. Take the shorter one from and to as the standard. For example:

Strtr ("aidenliu", "AI", "B") only replaces the output in the original string: bidenliu

Strtr ("aidenliu", "A", "BC") only replaces the output in the original string: bidenliu

This function is case-sensitive. If it is replaced multiple times, the blueprint for each replacement is the original string instead of the previous replacement, as shown in figure

Strtr ("aidenliu", "AA", "A @") Outputs aidenliu instead of @ idenliu

Strtr (string, array ):

Array is the associated array. Replace the corresponding key in the original string with the value in the associated array. If the key is replaced multiple times, each replacement is the original string, instead of replacing it with one replacement (this is different from str_replace)

Strtr (string, from, to) and strtr (string, array ):

Strtr (string, from, to) is replaced by characters, while strtr (string, array) is replaced by strings.

Strtr ("aidenliu", "AI", "") is not replaced (the second parameter is a string of zero length)

Strtr ("aidenliu", array ("AI" => "") is replaced

Refer to the following articles:


The replacement functions in PHP mainly include strtr () and str_repalce (). But do you know the differences and usage of these two functions?

Let's take a look at the two usage of the PHP string replacement function strtr:

Strtr (string, from,)

Or strtr (string, array)

First, the first method for strtr functions is as follows:

Let's take a look at the example below:

<? 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. 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.

<? PHP
Echo 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:

<? PHP
Echo 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 );
?>

Result:

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. Mlevine@adtraq.com (11-apr-1999) pointed out that in PHP 3.0.7, this function has some bugs, and the nadeem@bleh.org (05-jun-1999) supplement in PHP 3.0.8 version function will return to normal.

Example

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, and <p> change <HR>, clear all </P> [@ str_replace (Array ('<br>', '<p>', '</P> '), array ('<br/>', '<HR>', ''), $ content)]

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.