PHP string replacement function strtr () Implementation

Source: Internet
Author: User
Tags php operator

Today, we will talk about the strtr () function used to replace PHP Strings. For beginners, The strtr () function of the PHP string replacement function is still relatively simple, and we hope that the content introduced in this article will give you a deep understanding of the specific significance of this function.

    • Explanation of the execution time of PHP computing pagesCodeImplementation
    • PHP operator category Overview
    • Correct use of PHP quotation marks
    • Describes the composition of PHP Strings
    • Detailed description of the str_replace function of PHP

Let's take a look at the two statuses of the 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.

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.