Home > php教程 > php手册 > 高手教你PHP字符串截取函数使用

高手教你PHP字符串截取函数使用

WBOY
Release: 2016-06-13 11:05:24
Original
1167 people have browsed it

学习PHP时,你可能会遇到PHP字符串截取问题,这里将介绍PHP字符串截取函数问题的解决方法,在这里拿出来和大家分享一下。PHP本身是一种简单而强大的语言。PHP语言拥有核心特性如强大的字符串和数组处理能力,同时极大的改进了对面向对象编程的支持(PHP5以上版本)。

通过使用标准的和可选的扩展模块,PHP应用程序可以连接MySQL或Oracle等十几种数据库、绘图、创建PDF文件和创建解析XML文件。你也可以使用C语言来写自己的PHP扩展模块。例如,在已存在的代码库中提供一个PHP的接口函数。你也可以在Windows下运行PHP,使用COM控制其它诸如Word和Excel的Windows应用程序,或者使用ODBC来连接数据库。在国内,PHP曾经和微软的ASP并驾齐驱,是大家常用的网络编程语言。

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>
Copy after login

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template