Rumah > Artikel > pembangunan bahagian belakang > PHP Substr库函数的功能介绍_PHP教程
初学下面这个PHP Substr库函数程序不算完美,但处理一般的中文(GB18030,GB2312,BIG5)是没有问题的。这个函数不适合utf-8编码的文字。
<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>$max && $index</span><span class="tag"><span class="tag-name">strlen</span><span>($str)){ </span></span></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>=0xfe){ </span></span> </li> <li> <span>if($index+1</span><span class="tag"><span class="tag-name">strlen</span><span>($str)){ </span></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 class="tag-name">0x7e</span><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>=0xfe) ){ </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>//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>=0xf7){ </span></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 class="tag-name">strlen</span><span>($str)){ </span></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 class="tag-name">0xfe</span><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>
以上代码示例就是PHP Substr库函数在截取中文字符时的具体使用方法。