Home > php教程 > php手册 > 演示PHP substr函数使用

演示PHP substr函数使用

WBOY
Release: 2016-06-13 11:05:07
Original
1067 people have browsed it

PHP是我们现在最常用的动态开发WEB页面的语言,字符串的处理在编程中是最长用到的,我们这里就详细的谈谈PHP substr的利用。有一堆类似以下变量$abc的字符串,末尾都多出来了一个,(逗号),我想去掉末尾的逗号,为了验证不同的处理方法的执行时间,以下是我写的一个对比程序进行试验:

程序一,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>
<li class=""><span>function getmicrotime(){  </span></li>
<li class="alt"><span>list($usec, $sec) = explode(" ",microtime());  </span></li>
<li class=""><span>return ((float)$usec + (float)$sec);  </span></li>
<li class="alt"><span>}  </span></li>
<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>
</li>
<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>
</li>
<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>
</li>
<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>
</li>
<li class="">
<span>echo $abc."</span><strong><font color="#006699"><span class="tag"><span class="tag-name">br</span><span class="tag">></span></span></font></strong><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

程序二,利用正则表达式

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

结果程序一的执行平均时间为0.045,程序二的执行平均时间为0.085。不进行任何处理的平均执行时间为0.041。可见利用第一种方法处理效率要高出尽50%。由此,我们在做程序的过程中,尤其是在要求高效的程序中,我们应该对不同的方法进行一下对比,找出最优化的处理方法,以上就是PHP substr 和正则表达式的程序实例。
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template