PHP replaces string functions strtr () and str

Source: Internet
Author: User
Strtr (string, from, to): Start to replace each character. Take the shorter character from and to as the standard. For example: strtr (aidenliu, ai, B) only Replace the output in the original string: bidenliustrtr (aidenliu, a, bc) only replace the output in the original string: bidenliu. This function is case-sensitive.

Strtr (string, from, to): Start to replace each character. Take the shorter character from and to as the standard. For example: strtr (aidenliu, ai, B) only replace a output in the original string: bidenliu strtr (aidenliu, a, bc) only replace a output in the original string: bidenliu this function is case sensitive, if many

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:

 

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.

 

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:

 

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

 

Result:

I Love her sister

7. Tips: If you want to replace something, add it to the array.

For example:

 

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

 

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 replace all

','

'), '', $ Content)]

One-to-one replacement: to replace all

[@ Str_replace ('
','

', $ Content)]

Many-to-many replacement: to replace
Change
, At the same time

Change

Clear all [@ str_replace (array ('
','

','

'), Array ('
','

', ''), $ 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.