>백엔드 개발 >PHP 튜토리얼 >演示PHP substr函数使用_PHP教程

演示PHP substr函数使用_PHP教程

WBOY
WBOY원래의
2016-07-15 13:27:531067검색

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>

程序二,利用正则表达式

<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>

结果程序一的执行平均时间为0.045,程序二的执行平均时间为0.085。不进行任何处理的平均执行时间为0.041。可见利用第一种方法处理效率要高出尽50%。由此,我们在做程序的过程中,尤其是在要求高效的程序中,我们应该对不同的方法进行一下对比,找出最优化的处理方法,以上就是PHP substr 和正则表达式的程序实例。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446480.htmlTechArticlePHP是我们现在最常用的动态开发WEB页面的语言,字符串的处理在编程中是最长用到的,我们这里就详细的谈谈PHP substr的利用。有一堆类似以...
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.