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