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>