PHPは、Webページを動的に開発するために最も一般的に使用される言語です。ここでは、PHPのsubstrの使用方法について詳しく説明します。以下の変数 $abc のような文字列が大量にあり、末尾に余分なカンマがあります。さまざまな処理メソッドの実行時間を確認するために、次のようにします。テスト用に作成した比較プログラム:
プログラム 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>
プログラム 2、正規表現を使用
<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>