PHP replaces functions mainly with several functions Strtr (), str_repalce ().

Source: Internet
Author: User

PHP Replace function mainly has STRTR (), str_repalce () These two functions, today introduced their differences and usage, first look at this PHP string substitution function Strtr () two usages:

Strtr (string,from,to) or Strtr (string,array) first for the STRTR function first way:

Let's take a look at the following example:

<?php
echo strtr ("I love you", "lo", "lo");
?>

The result is: 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);
?>

The Result: 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: 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 replaces functions mainly with several functions Strtr (), str_repalce ().

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.