PHP string replacement function strtr () implementation _ PHP Tutorial

Source: Internet
Author: User
PHP string replacement function strtr. What we want to talk about today is about the two statuses strtr (string, from, to) of the php string replacement function strtr) or strtr (string, arr. what we want to talk about today isLet's take a look at the two statuses of the php string replacement function PHP string replacement function strtr ().

Strtr (string, from,)

Or strtr (string, array)

First, the first method for replacing the strtr () function with a PHP string

Let's take a look at the example below:

 
 
  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.

2. the replacement of strtr () in the PHP string replacement function is very special. yOu should note that yOu and the O in the middle are replaced later. this is obviously not our intention.

Here is another special example to show how strange this php sttr function is.

 
 
  1. php
  2. echo strtr("I Love you","Love","");
  3. ?>

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 replacing strtr () with a PHP string

 
 
  1. <,balencigag handbag;?php
  2. echo strtr("I Loves you","Love","lOvEA");
  3. ?>

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 the strtr () function with a PHP string

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. PHP string replacement function strtr ()

 
 
  1. php
  2. $table_change = array('you'=>'her sister');
  3. echo strtr("I Love you",$table_change);
  4. ?>

Result:

I Love her sister

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

 
 
  1. php
  2. $table_change = array('you'=>'her 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 writing Love is not feasible.

Okay. let's talk about it in a mess. In fact, what strtr wants to talk about is the subsequent usage.

Simple and convenient.

It seems that the subsequent usage also ignores the problem of different front and back character lengths.

The above PHP string replacement function strtr () experiment, PHP5.2 test passed.


Let's take a look at this php string replacement function PHP string replacement function strtr () two statuses of 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.