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>
The above code example is the specific use of the PHP Substr library function when intercepting Chinese characters.