PHP Replacement String function Strtr () and str_repalce () differences

Source: Internet
Author: User

The substitution functions in PHP mainly have Strtr (), Str_repalce (), but do you all know the difference and usage of these two functions?

Let's take a look at two uses of this PHP string substitution function strtr ():

STRTR (String,from,to)

or STRTR (String,array)

First, for the STRTR function, the first way:

Let's take a look at the following example:

<?php
Echo strtr ("I Love You", "Lo", "Lo");
?>

The resulting results are:

I Love You

This result reminds us:

1.STRTR It is case-sensitive
The replacement of 2.STRTR is very special, you look at the back that you, the middle O is replaced, this is obviously not our intention.
Give a special example of this PHP sttr function of the weird

<?php
Echo strtr ("I Love You", "Love", "" ");
?>

The result is:

I Love You

Nothing will change, so STRTR need to be aware of:

3. Can not be replaced with empty, that is, the final argument cannot be an empty string, of course, the space is possible.

Another example of the STRTR function:

<?php
Echo strtr ("I Loves You", "Love", "Lovea");
?>

The result is:

I LOvEs You

Notice that a of the third parameter does not appear in the result.

4. I do not recommend using STRTR to change the number less.

OK, since this strtr function is very 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. Be sure to use the STRTR function when using it.

How do you feel comfortable with that?

This is the second case of its kind:

STRTR (String,array)

6.STRTR Compliant Use method

<?php
$table _change = Array (' you ' + = ' her sister ');
Echo strtr ("I Love You", $table _change);
?>

Variables can also be used in arrays, such as: $table _change = Array ($old = =$new);

The result is:

I Love her sister

7. Tips: You want to replace what you go to the array plus what

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

Remind me again that love is not a good thing to write about.

String substitution.

Syntax: string str_replace (String needle, String str, String haystack);

return value: String

Function type: Data processing

Content Description

This function converts the string str into the haystack string, replacing 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", variable name of the replaced content)]
[@str_replace (' Old 1 ', ' Old 2 ', ' Old 3 '), Array (' New 1 ', ' New 2 ', ' new 3 '), $ replaced by the variable name of the content)]
[@str_replace (Array (' Old 1 ', ' Old 2 ', ' Old 3 '), ' new content ', ' variable name of the replaced content ')]

Instance:

Many-to-one substitution: To remove all <p></p> tags in the Content field, replace with empty [@str_replace (' <p> ', ' </p> '), ', $Content)]

One-to-one replacement: To change all <br> tags in the Content field to <p> [@str_replace (' <br> ', ' <p> ', $Content)]

Many-to-many substitutions: Want to change <br> in the Content field to <br/>, while <p> change

PHP Replacement String function Strtr () and str_repalce () differences

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.