Detailed usage of strtr string replacement in PHP, strtr string _ PHP Tutorial

Source: Internet
Author: User
Detailed usage of strtr string replacement in PHP, strtr string. The strtr string replacement usage in PHP is described in detail. This document describes how to replace strtr strings in PHP. Share it with you for your reference. The detailed analysis is as follows: strtr (string, strtr string replacement usage in PHP, strtr string

This example describes how to replace strtr strings in PHP. Share it with you for your reference. The specific analysis is as follows:

Strtr (string, from, to) or strtr (string, array)

First, let's look at the following example for the first method of strtr function. the code is as follows:

The code is as follows:

<? Php
Echo strtr ("I Love you", "Lo", "lO ");
?>


The result is:
1 I lOve yOu
This result reminds us: 1. strtr: it is case sensitive. 2. the replacement of strtr is very special. yOu should note that the O in the middle of yOu is replaced. this is obviously not our intention. let's take another special example, the code for this php sttr function is as follows:

The code is as follows:

<? Php
Echo strtr ("I Love you", "Love ","");
?>


The result is:
I Love you
Nothing will change, so strtr should note that it cannot be replaced with Null, that is, the last parameter cannot be a null string. of course, spaces are acceptable, the code is as follows:

The code is as follows:

<? Php
Echo strtr ("I Loves you", "Love", "lOvEA ");
?>


The running result is
1 I lOvEs yOu
Note that A of the third parameter does not appear in the result.

I don't recommend using strtr to replace more with less. 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, so it must be used when the strtr function is used. how can it be used comfortably? This is its second case: strtr (string, array)

The code of strtr is as follows:

The code is as follows:

<? Php
$ Table_change = array ('you' => 'Her sister ');
Echo strtr ("I Love you", $ table_change );
?>


Result:
I Love her sister
Tips: add the following code to the array if you want to replace it:

The code is as follows:

<? 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
Remind me again that writing Love into love is not feasible. In fact, what strtr wants to talk about is that the subsequent usage is simple and convenient, it seems that the subsequent usage also ignores the problem of different front and back character lengths. the above php strtr function experiment, PHP5.2 passed the test, the code is as follows:

The code is as follows:

<? Php
// At this time, the output is baicai instead of 2017123cai, because str ("pao ") Echo strtr ("paocai! "," Pao "," 2017123 ");
?>


Instance 2: When the length of the replaced value is smaller than the target, the following is the reference content. the code is as follows:

The code is as follows:

<? Php
// At this time, laocai is output instead of lacai, because str ("pao")> strlen ("la ")
Echo strtr ("paocai! "," Pao "," la ");
?>


Example 3: Array replacement is supported. the reference content is as follows:

The code is as follows:

<? Php
$ Arr = array ('ao' => 'A', 'Ai' => 'A ');
Echo strtr ("paocai! ", $ Arr); // The output is poacia.
?>


The second is str_replace. the reference content is as follows:

The code is as follows:

<? Php
Echo str_replace ("you", "paocai", "I love you! "); // Will output I love paocai!
?>

I hope this article will help you with PHP programming.

Examples in this article describes how to replace strtr strings in PHP. Share it with you for your reference. The specific analysis is as follows: strtr (string ,...

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.