Introduction to the functions of PHP Substr library function_PHP tutorial

WBOY
Release: 2016-07-15 13:31:30
Original
1083 people have browsed it

BeginnerThe following PHP Substr library function program is not perfect, but there is no problem in processing general Chinese (GB18030, GB2312, BIG5). This function is not suitable for UTF-8 encoded text.

<ol class="dp-xml">
<li class="alt"><span><span>//$str字符串  </span></span></li>
<li><span>//$max 最大字符数  </span></li>
<li class="alt"><span>function Substring($str,$max){  </span></li>
<li>
<span>$</span><span class="attribute">cnt</span><span>=</span><span class="attribute-value">0</span><span>; //实际计数  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">index</span><span>=</span><span class="attribute-value">0</span><span>; //当前索引  </span>
</li>
<li>
<span>$</span><span class="attribute">output</span><span>=</span><span class="attribute-value">''</span><span>; //输出  </span>
</li>
<li class="alt"><span>//  </span></li>
<li>
<span>while($cnt</span><span class="tag"><</span><span>$max && $index</span><span class="tag"><</span><span class="tag-name">strlen</span><span>($str)){  </span></li><li class="alt"><span>$</span><span class="attribute">output.</span><span>=$str[$index];  </span></li><li><span>//big5  </span></li><li class="alt"><span>if(ord($str[$index])</span><span class="tag">></span><span>=0x81 &&<br> ord($str[$index])</span><span class="tag"><</span><span>=0xfe){  </span></li><li><span>if($index+1</span><span class="tag"><</span><span class="tag-name">strlen</span><span>($str)){  </span></li><li class="alt"><span>if( (ord($str[$index+1])</span><span class="tag">></span><span>=0x40 <br>&& ord($str[$index+1])</span><span class="tag"><</span><span class="tag-name">0x7e</span><span>)   </span></li><li><span>|| (ord($str[$index+1])</span><span class="tag">></span><span>=0xa1 <br>&& ord($str[$index+1])</span><span class="tag"><</span><span>=0xfe) ){  </span></li><li class="alt"><span>$index++;  </span></li><li><span>$</span><span class="attribute">output.</span><span>=$str[$index];  </span></li><li class="alt"><span>}  </span></li><li><span>}  </span></li><li class="alt"><span>}  </span></li><li><span>//gb2312  </span></li><li class="alt"><span>else if(ord($str[$index])</span><span class="tag">></span><span>=0xa1<br> && ord($str[$index])</span><span class="tag"><</span><span>=0xf7){  </span></li><li><span>$</span><span class="attribute">output.</span><span>=$str[$index];  </span></li><li class="alt"><span>if($index+1</span><span class="tag"><</span><span class="tag-name">strlen</span><span>($str)){  </span></li><li><span>if(ord($str[$index+1])</span><span class="tag">></span><span>=0xa1 <br>&& ord($str[$index+1])</span><span class="tag"><</span><span class="tag-name">0xfe</span><span>){  </span>
</li>
<li class="alt"><span>$index++;  </span></li>
<li>
<span>$</span><span class="attribute">output.</span><span>=$str[$index];  </span>
</li>
<li class="alt"><span>}  </span></li>
<li><span>}  </span></li>
<li class="alt"><span>}  </span></li>
<li><span>else{   </span></li>
<li class="alt"><span>}  </span></li>
<li><span>$cnt++;  </span></li>
<li class="alt"><span>$index++;  </span></li>
<li><span>}  </span></li>
<li class="alt"><span>return $output;  </span></li>
<li><span>}  </span></li>
</ol>
Copy after login

The above code example is the specific use of the PHP Substr library function when intercepting Chinese characters.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446214.htmlTechArticleThe following PHP Substr library function program is not perfect for beginners, but it can handle general Chinese (GB18030, GB2312, BIG5) There is no problem. This function is not suitable for UTF-8 encoded text. //$str characters...
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