Demonstrate the use of PHP substr function_PHP tutorial

WBOY
Release: 2016-07-15 13:27:53
Original
1062 people have browsed it

PHP is the most commonly used language for dynamically developing WEB pages. String processing is the most frequently used in programming. Here we will talk about the use of PHP substr in detail. There are a bunch of strings similar to the following variable $abc, and there is an extra comma at the end. I want to remove the comma at the end. In order to verify the execution time of different processing methods, the following is a comparison program I wrote for testing:

Program 1, PHP substr

<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>function getmicrotime(){  </SPAN><LI class=alt><SPAN>list($usec, $sec) = explode(" ",microtime());  </SPAN><LI class=""><SPAN>return ((float)$usec + (float)$sec);  </SPAN><LI class=alt><SPAN>}  </SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>time_start</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>getmicrotime</FONT></SPAN><SPAN>();  </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>abc</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"重庆,万州,涪陵,渝中,大渡口,江北,沙坪坝,九龙坡,南岸,北碚,万盛,双挢,渝北,巴南,黔江,长寿,綦江,潼南,铜梁,<br>大足,荣昌,壁山,梁平,城口,丰都,垫江,武隆,忠县,开县,云阳,奉节,巫山,巫溪,石柱,"</FONT></SPAN><SPAN>;  </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>l</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>strlen</FONT></SPAN><SPAN>($abc);  </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>abc</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>substr</FONT></SPAN><SPAN>($abc,0,$l-1);  </SPAN></SPAN><LI class=""><SPAN>echo $abc."</SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>></span></font></strong><span>";  </span></span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">time_end</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">getmicrotime</font></span><span>();  </span>
</li>
<li class=""><span>echo ($time_end-$time_start)*1000;  </span></li>
<li class="alt">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span>
</li>
</ol>
Copy after login

Program 2, using regular expressions

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN><SPAN> </SPAN></SPAN><LI class=""><SPAN>function getmicrotime(){  </SPAN><LI class=alt><SPAN>list($usec, $sec) = explode(" ",microtime());  </SPAN><LI class=""><SPAN>return ((float)$usec + (float)$sec);  </SPAN><LI class=alt><SPAN>}  </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute>time_start</SPAN><SPAN> = </SPAN><SPAN class=attribute-value>getmicrotime</SPAN><SPAN>();  </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute>abc</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>"重庆,万州,涪陵,渝中,大渡口,江北,沙坪坝,九龙坡,南岸,北碚,万盛,双挢,渝北,巴南,黔江,长寿,綦江,潼南,铜梁,<br>大足,荣昌,壁山,梁平,城口,丰都,垫江,武隆,忠县,开县,云阳,奉节,巫山,巫溪,石柱,"</SPAN><SPAN>;  </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute>abc</SPAN><SPAN>=</SPAN><SPAN class=attribute-value>str_replace</SPAN><SPAN>(",$","",$abc);  </SPAN></SPAN><LI class=alt><SPAN>echo $abc."</SPAN><SPAN class=tag><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>></span><span>";  </span></span></li>
<li class="">
<span>$</span><span class="attribute">time_end</span><span> = </span><span class="attribute-value">getmicrotime</span><span>();  </span>
</li>
<li class="alt"><span>echo ($time_end-$time_start)*1000;  </span></li>
<li class="">
<span></span><span class="tag">?></span><span> </span>
</li>
</ol>
Copy after login

As a result, the average execution time of program one is 0.045, and the average execution time of program two is 0.085. The average execution time without any processing is 0.041. It can be seen that the processing efficiency of using the first method is up to 50% higher. Therefore, when we are making programs, especially in programs that require high efficiency, we should compare different methods and find the most optimized processing method. The above are program examples of PHP substr and regular expressions.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446480.htmlTechArticlePHP is our most commonly used language for dynamically developing WEB pages. String processing is the longest in programming. Used, we will talk about the use of PHP substr in detail here. There are a bunch of things similar to...
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