学习PHP时,你可能会遇到PHP字符串截取问题,这里将介绍PHP字符串截取函数问题的解决方法,在这里拿出来和大家分享一下。PHP本身是一种简单而强大的语言。PHP语言拥有核心特性如强大的字符串和数组处理能力,同时极大的改进了对面向对象编程的支持(PHP5以上版本)。
ASP3.0现在早已经被微软放弃了,不再更新,也许有人会认为:PHP应该也是个快作古的东西了吧?然而事实恰恰相反,PHP不仅在高速发展,把ASP远远抛在身后,而且渐渐成为现在互联网上使用最为广泛、最热门的语言。下面我们就一起利用PHP这门强大的语言解决这个问题吧。
PHP字符串截取函数代码:
<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> </span></li> <li class="alt"> <span>functionget_substr($string,$</span><span class="attribute">start</span><span>=</span><span class="attribute-value">'0'</span><span>,$</span><span class="attribute">length</span><span>=</span><span class="attribute-value">''</span><span>) </span> </li> <li class=""><span>{ </span></li> <li class="alt"> <span>$</span><span class="attribute">start</span><span>=(int)$start; </span> </li> <li class=""> <span>$</span><span class="attribute">length</span><span>=(int)$length; </span> </li> <li class="alt"> <span>$</span><span class="attribute">i</span><span>=</span><span class="attribute-value">0</span><span>; </span> </li> <li class=""><span>if(!$string) </span></li> <li class="alt"><span>{ </span></li> <li class=""><span>return; </span></li> <li class="alt"><span>} </span></li> <li class=""> <span>if($start</span><span class="tag">></span><span>=0) </span> </li> <li class="alt"><span>{ </span></li> <li class=""> <span>while($i</span><span class="tag"><span>$start) </span></span> </li> <li class="alt"><span>{ </span></li> <li class=""> <span>if(ord($string[$i])</span><span class="tag">></span><span>127) </span> </li> <li class="alt"><span>{ </span></li> <li class=""> <span>$</span><span class="attribute">i</span><span>=$i+2; </span> </li> <li class="alt"><span>} </span></li> <li class=""><span>else </span></li> <li class="alt"><span>{ </span></li> <li class=""><span>$i++; </span></li> <li class="alt"><span>} </span></li> <li class=""><span>} </span></li> <li class="alt"> <span>$</span><span class="attribute">start</span><span>=$i; </span> </li> <li class=""> <span>if($</span><span class="attribute">length</span><span>=='') </span> </li> <li class="alt"><span>{ </span></li> <li class=""><span>returnsubstr($string,$start); </span></li> <li class="alt"><span>} </span></li> <li class=""> <span>elseif($length</span><span class="tag">></span><span>0) </span> </li> <li class="alt"><span>{ </span></li> <li class=""> <span>$</span><span class="attribute">end</span><span>=$start+$length; </span> </li> <li class="alt"> <span>while($i</span><span class="tag"><span>$end) </span></span> </li> <li class=""><span>{ </span></li> <li class="alt"> <span>if(ord($string[$i])</span><span class="tag">></span><span>127) </span> </li> <li class=""><span>{ </span></li> <li class="alt"> <span>$</span><span class="attribute">i</span><span>=$i+2; </span> </li> <li class=""><span>} </span></li> <li class="alt"><span>else </span></li> <li class=""><span>{ </span></li> <li class="alt"><span>$i++; </span></li> <li class=""><span>} </span></li> <li class="alt"><span>} </span></li> <li class=""><span>if($end!=$i-1) </span></li> <li class="alt"><span>{ </span></li> <li class=""> <span>$</span><span class="attribute">end</span><span>=$i; </span> </li> <li class="alt"><span>} </span></li> <li class=""><span>else </span></li> <li class="alt"><span>{ </span></li> <li class=""><span>$end--; </span></li> <li class="alt"><span>} </span></li> <li class=""> <span>$</span><span class="attribute">length</span><span>=$end-$start; </span> </li> <li class="alt"><span>returnsubstr($string,$start,$length); </span></li> <li class=""><span>} </span></li> <li class="alt"> <span>elseif($</span><span class="attribute">length</span><span>==0) </span> </li> <li class=""><span>{ </span></li> <li class="alt"><span>return; </span></li> <li class=""><span>} </span></li> <li class="alt"><span>else </span></li> <li class=""><span>{ </span></li> <li class="alt"> <span>$</span><span class="attribute">length</span><span>=</span><span class="attribute-value">strlen</span><span>($string)-abs($length)-$start; </span> </li> <li class=""><span>returnget_substr($string,$start,$length); </span></li> <li class="alt"><span>} </span></li> <li class=""><span>} </span></li> <li class="alt"><span>else </span></li> <li class=""><span>{ </span></li> <li class="alt"> <span>$</span><span class="attribute">start</span><span>=</span><span class="attribute-value">strlen</span><span>($string)-abs($start); </span> </li> <li class=""><span>returnget_substr($string,$start,$length); </span></li> <li class="alt"><span>} </span></li> <li class=""><span>} </span></li> <li class="alt"><span> </span></li> <li class=""> <span></span><span class="tag">?></span><span> </span> </li> </ol>