What is the difference between PHP replacement string function Strtr () and Str_repalce ()

Source: Internet
Author: User

The substitution function in PHP mainly has STRTR (), Str_repalce () These two functions, today introduces their difference and usage, first take a look at this PHP string substitution function strtr () Two kinds of usage:

STRTR (string,from,to) or STRTR (String,array) first 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:
STRTR (String,array)
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.
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.
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, replace them with empty [@str_replace (Array (' <p> ', ' </p> '), ', $Content]

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

Multi-Multiple substitution: Want to change <br> in content field to <br/>, while <p> for

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.