Home > Backend Development > PHP Tutorial > 一个中文字符串截取函数_PHP

一个中文字符串截取函数_PHP

WBOY
Release: 2016-06-01 12:25:49
Original
848 people have browsed it
PHP代码:
<font color="#000000"><br><br><br><br><br><font color="#0000bb"><?php <br /><br><br><br></font><font color="#ff8000">/**<br><br>***@Author:LAD<br><br>***@URL   :<a href="http://www.cnpik.com/" target="_blank">http://www.cnpik.com/</a><br><br>***@E_mail:lianxiwoo@sohu.com <br><br>***@随便用,不收钱 :-)<br><br>*/<br><br><br><br></font><font color="#007700">function </font><font color="#0000bb">cnSubStr</font><font color="#007700">(</font><font color="#0000bb">$string</font><font color="#007700">,</font><font color="#0000bb">$sublen</font><font color="#007700">)<br><br>{<br><br>    if(</font><font color="#0000bb">$sublen</font><font color="#007700">>=</font><font color="#0000bb">strlen</font><font color="#007700">(</font><font color="#0000bb">$string</font><font color="#007700">))<br><br>    {<br><br>        return </font><font color="#0000bb">$string</font><font color="#007700">;<br><br>    }<br><br>    </font><font color="#0000bb">$s</font><font color="#007700">=</font><font color="#dd0000">""</font><font color="#007700">;<br><br>    for(</font><font color="#0000bb">$i</font><font color="#007700">=</font><font color="#0000bb">0</font><font color="#007700">;</font><font color="#0000bb">$i</font><font color="#007700"><font color="#0000bb">$sublen</font><font color="#007700">;</font><font color="#0000bb">$i</font><font color="#007700">++)<br><br>    {<br><br>        if(</font><font color="#0000bb">ord</font><font color="#007700">(</font><font color="#0000bb">$string</font><font color="#007700">{</font><font color="#0000bb">$i</font><font color="#007700">})></font><font color="#0000bb">127</font><font color="#007700">) <br><br>        {<br><br>            </font><font color="#0000bb">$s</font><font color="#007700">.=</font><font color="#0000bb">$string</font><font color="#007700">{</font><font color="#0000bb">$i</font><font color="#007700">}.</font><font color="#0000bb">$string</font><font color="#007700">{++</font><font color="#0000bb">$i</font><font color="#007700">};<br><br>            continue;<br><br>        }else{<br><br>            </font><font color="#0000bb">$s</font><font color="#007700">.=</font><font color="#0000bb">$string</font><font color="#007700">{</font><font color="#0000bb">$i</font><font color="#007700">};<br><br>            continue;<br><br>        } <br><br>    }<br><br>    return </font><font color="#0000bb">$s</font><font color="#007700">;<br><br>}</font><font color="#ff8000">// End Function cnSubStr($string,$sublen)<br><br><br><br>/////////// Use like this :<br><br><br><br></font><font color="#007700">echo </font><font color="#dd0000">"<p>__________________________</p> <p>"</p></font><font color="#007700">;<br><br></font><font color="#0000bb">$string</font><font color="#007700">=</font><font color="#dd0000">"242432反对感是456犯得上广泛大使馆地方7890"</font><font color="#007700">;<br><br></font><font color="#0000bb">$sublen</font><font color="#007700">=</font><font color="#0000bb">strlen</font><font color="#007700">(</font><font color="#0000bb">$string</font><font color="#007700">);<br><br></font><font color="#0000bb">$len</font><font color="#007700">=</font><font color="#0000bb">20</font><font color="#007700">;<br><br>echo </font><font color="#0000bb">$string</font><font color="#007700">.</font><font color="#dd0000">"<p>"</p></font><font color="#007700">;<br><br>echo </font><font color="#dd0000">"总长为:"</font><font color="#007700">.(</font><font color="#0000bb">$sublen</font><font color="#007700">+</font><font color="#0000bb">1</font><font color="#007700">).</font><font color="#dd0000">"<p>"</p></font><font color="#007700">;<br><br>echo </font><font color="#dd0000">"截取数:"</font><font color="#007700">.</font><font color="#0000bb">$len</font><font color="#007700">.</font><font color="#dd0000">"<p>"</p></font><font color="#007700">;<br><br><br><br>for(</font><font color="#0000bb">$i</font><font color="#007700">=</font><font color="#0000bb">1</font><font color="#007700">;</font><font color="#0000bb">$i</font><font color="#007700"><font color="#0000bb">$sublen</font><font color="#007700">+</font><font color="#0000bb">1</font><font color="#007700">;</font><font color="#0000bb">$i</font><font color="#007700">++){<br><br>    if(</font><font color="#0000bb">$i</font><font color="#007700">></font><font color="#0000bb">$len</font><font color="#007700">){<br><br>        echo </font><font color="#0000bb">$i</font><font color="#007700">.</font><font color="#dd0000">"<b> →</b> "</font><font color="#007700">.</font><font color="#0000bb">cnSubStr</font><font color="#007700">(</font><font color="#0000bb">$string</font><font color="#007700">,</font><font color="#0000bb">$i</font><font color="#007700">).</font><font color="#dd0000">"…<br>"</font><font color="#007700">;<br><br>        continue;<br><br>    }<br><br>    echo </font><font color="#0000bb">$i</font><font color="#007700">.</font><font color="#dd0000">"<b> →</b> "</font><font color="#007700">.</font><font color="#0000bb">cnSubStr</font><font color="#007700">(</font><font color="#0000bb">$string</font><font color="#007700">,</font><font color="#0000bb">$i</font><font color="#007700">).</font><font color="#dd0000">"<br>"</font><font color="#007700">;<br><br>}<br><br><br><br></font><font color="#0000bb">?><br><br></font></font><br></font></font>
Related labels:
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template