Home > Backend Development > PHP Tutorial > Explanation of the function implementation of PHP string replacement function strtr()_PHP tutorial

Explanation of the function implementation of PHP string replacement function strtr()_PHP tutorial

WBOY
Release: 2016-07-15 13:30:05
Original
1108 people have browsed it

What we are going to tell you today is about Let’s first take a look at the two states of this php string replacement function PHP string replacement function strtr()

strtr(string,from,to)

or strtr(string,array)

First, replace the function strtr() in PHP string in the first way

Let’s look at the following example:

<ol class="dp-xml"><li class="alt">
<span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN>echo strtr("I Love you","Lo","lO");  </SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong></span><span> </span>
</li></ol>
Copy after login

The result obtained is

I lOve yOu

This result reminds us

1.strtr It is case-sensitive

2. The replacement of PHP string replacement function strtr() is very special. Please pay attention to the yOu at the back, the O in the middle is replaced , this is obviously not our intention

Give another special example to illustrate the weirdness of this php sttr function

<ol class="dp-xml"><li class="alt">
<span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN>echo strtr("I Love you","Love","");  </SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong></span><span> </span>
</li></ol>
Copy after login

The result is

I Love you

Nothing will change, so what strtr needs to pay attention to is:

3. It cannot be Replace it with nothing, that is, the last parameter cannot be an empty string, of course, spaces are allowed.

Another example of another case of PHP string replacement function strtr()

<ol class="dp-xml"><li class="alt"><span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN>,balencigag handbag;?php  </SPAN></SPAN><LI class=""><SPAN>echo strtr("I Loves you","Love","lOvEA");  </SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></font></strong></span><span> </span></span></li></ol>
Copy after login

The result is

I lOvEs yOu

Note that A in the third parameter does not appear in the result

4 .I do not recommend using PHP string replacement function strtr() to exchange less for more

ok. Since this strtr function is quite troublesome, why use it?

The reason is that it is very fast

It is said that strtr is four times faster than str_replace

5. When you can use the strtr function, you must use it

So what? Is it comfortable to use?

This is its second case

strtr(string,array)

6. How to use the PHP string replacement function strtr() as you wish

<ol class="dp-xml">
<li class="alt">
<span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>table_change</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>array</FONT></SPAN><SPAN>('you'=</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong></span><span>'her sister');  </span>
</li>
<li class="alt"><span>echo strtr("I Love you",$table_change);  </span></li>
<li class="">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span>
</li>
</ol>
Copy after login

The result is

I Love her sister

7. Tips: Just add whatever you want to replace to the array

<ol class="dp-xml">
<li class="alt">
<span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>table_change</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>array</FONT></SPAN><SPAN>('you'=</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong></span><span>'her sister');  </span>
</li>
<li class="alt">
<span>$table_change += array('Love' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'hate');  </span>
</li>
<li class=""><span>echo strtr("I Love you",$table_change);  </span></li>
<li class="alt">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span>
</li>
</ol>
Copy after login

The result is

I hate her sister

Remind me again that Love is written as love. It doesn't work.

Okay, I talked a lot. In fact, what I want to say most about strtr is the usage behind it.

It is simple and convenient.

It seems that the later usage also ignores the problem of different character lengths before and after

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


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446319.htmlTechArticleWhat we are going to talk to you about today is about first taking a look at this php string replacement function PHP string replacement The two states of function strtr() strtr(string,from,to) or strtr(string,arr...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template