function implementation of PHP string substitution function strtr () _php Tutorial

Source: Internet
Author: User
What we're going to talk to you about today is aboutLet's take a look at this php string substitution function php string substitution function strtr () two states

STRTR (String,from,to)

or STRTR (String,array)

First for PHP string substitution function strtr () First Way

Let's take a look at the following example:

 
  
  
  1. Php
  2. Echo strtr ("I Love You", "Lo", "Lo");
  3. ?>

The result is

I Love You

This result reminds us

1.STRTR It is case-sensitive

The substitution of the 2.PHP string substitution function strtr () is very special, you notice the back that you, the middle O is replaced, this is obviously not our intention

Give a special example of the weird php sttr function

 
  
  
  1. Php
  2. Echo strtr ("I Love You", "Love", "" ");
  3. ?>

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.

Again example PHP string substitution function strtr () another case

 
  
  
  1. < , Balencigag handbag;? PHP
  2. Echo strtr ("I Loves You", "Love", "Lovea");
  3. ?>

The result is

I LOvEs You

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

4. I do not recommend replacing the function strtr () with PHP string to change the number less

OK, since this strtr function is very troublesome why use it?

The reason is that it's fast

STRTR is said to be four times times faster than Str_replace.

5. Be sure to use the STRTR function when using

How do you feel comfortable with that?

This is the second case of it.

STRTR (String,array)

6.PHP string substitution function strtr () to use with intent

 
  
  
  1. Php
  2. $ Table_change Array(' You ' =>' she sister ');
  3. Echo strtr ("I Love You", $table _change);
  4. ?>

Result is

I Love her sister

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

 
  
  
  1. Php
  2. $ Table_change Array(' You ' =>' she sister ');
  3. $table _change + = Array (' love ' => ' hate ') ;
  4. Echo strtr ("I Love You", $table _change);
  5. ?>

The result is

I hate her sister

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

OK, a mess said a pass, in fact, about STRTR most want to say is this behind the use of

Simple and convenient.

It seems that the following usage also ignores the problem of different character lengths.

Above PHP string substitution function strtr () experiment, PHP5.2 test passed.


http://www.bkjia.com/PHPjc/446319.html www.bkjia.com true http://www.bkjia.com/PHPjc/446319.html techarticle What we're going to talk about today is about the two states of the PHP string substitution function Strtr () STRTR (string,from,to) or STRTR (String,arr ...

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