我们对关于utf-8的原理请看 UTF-8 FAQ
UTF-8编码的字符可能由1~3个字节组成, 具体数目可以由第一个字节判断出来。(理论上可能更长,但这里假设不超过3个字节)
第一个字节大于224的,它与它之后的2个字节一起组成一个UTF-8字符
第一个字节大于192小于224的,它与它之后的1个字节组成一个UTF-8字符
否则第一个字节本身就是一个英文字符(包括数字和一小部分标点符号)。
以前为某网站设计的代码(也是现在用在首页的长度截取的函数)
PHP截取utf-8字符串的代码示例:
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span> ?php // Cut_Str; </span></span></span></li> <li><span>//$sourcestr 是要处理的字符串 </span></li> <li class="alt"><span>//$cutlength 为截取的长度(即字数) </span></li> <li><span>function cut_str($sourcestr,$cutlength) </span></li> <li class="alt"><span>{ </span></li> <li> <span>$</span><span class="attribute">returnstr</span><span>=”; </span> </li> <li class="alt"> <span>$</span><span class="attribute">i</span><span>=</span><span class="attribute-value">0</span><span>; </span> </li> <li> <span>$</span><span class="attribute">n</span><span>=</span><span class="attribute-value">0</span><span>; </span> </li> <li class="alt"> <span>$</span><span class="attribute">str_length</span><span>=</span><span class="attribute-value">strlen</span><span>($sourcestr);//字符串的字节数 </span> </li> <li> <span>while (($n</span><span class="tag"><span>$cutlength) and ($i</span><span class="tag"><span>=$str_length)) </span></span></span> </li> <li class="alt"><span>{ </span></li> <li> <span>$</span><span class="attribute">temp_str</span><span>=</span><span class="attribute-value">substr</span><span>($sourcestr,$i,1); </span> </li> <li class="alt"> <span>$</span><span class="attribute">ascnum</span><span>=</span><span class="attribute-value">Ord</span><span>($temp_str);//得到字符串中第$i位字符的ascii码 </span> </li> <li> <span>if ($ascnum</span><span class="tag">></span><span>=224) //如果ASCII位高与224, </span> </li> <li class="alt"><span>{ </span></li> <li> <span>$</span><span class="attribute">returnstr</span><span>=$returnstr.substr($sourcestr,$i,3); <br>//根据UTF-8编码规范,将3个连续的字符计为单个字符 </span> </li> <li class="alt"> <span>$</span><span class="attribute">i</span><span>=$i+3; //实际Byte计为3 </span> </li> <li><span>$n++; //字串长度计1 </span></li> <li class="alt"><span>} </span></li> <li> <span>elseif ($ascnum</span><span class="tag">></span><span>=192) //如果ASCII位高与192, </span> </li> <li class="alt"><span>{ </span></li> <li> <span>$</span><span class="attribute">returnstr</span><span>=$returnstr.substr($sourcestr,$i,2);<br> //根据UTF-8编码规范,将2个连续的字符计为单个字符 </span> </li> <li class="alt"> <span>$</span><span class="attribute">i</span><span>=$i+2; //实际Byte计为2 </span> </li> <li><span>$n++; //字串长度计1 </span></li> <li class="alt"><span>} </span></li> <li> <span>elseif ($ascnum</span><span class="tag">></span><span>=65 && $ascnum</span><span class="tag"><span>=90) <br>//如果是大写字母, </span></span> </li> <li class="alt"><span>{ </span></li> <li> <span>$</span><span class="attribute">returnstr</span><span>=$returnstr.substr($sourcestr,$i,1); </span> </li> <li class="alt"> <span>$</span><span class="attribute">i</span><span>=$i+1; //实际的Byte数仍计1个 </span> </li> <li><span>$n++; //但考虑整体美观,大写字母计成一个高位字符 </span></li> <li class="alt"><span>} </span></li> <li><span>else //其他情况下,包括小写字母和半角标点符号, </span></li> <li class="alt"><span>{ </span></li> <li> <span>$</span><span class="attribute">returnstr</span><span>=$returnstr.substr($sourcestr,$i,1); </span> </li> <li class="alt"> <span>$</span><span class="attribute">i</span><span>=$i+1; //实际的Byte数计1个 </span> </li> <li> <span>$</span><span class="attribute">n</span><span>=$n+0.5; //小写字母和半角标点等与半个高位字符宽… </span> </li> <li class="alt"><span>} </span></li> <li><span>} </span></li> <li class="alt"> <span>if ($str_length</span><span class="tag">></span><span>$cutlength){ </span> </li> <li> <span>$</span><span class="attribute">returnstr</span><span> = $returnstr . “…”;<br>//超过长度时在尾处加上省略号 </span> </li> <li class="alt"><span>} </span></li> <li><span>return $returnstr; </span></li> <li class="alt"><span>} </span></li> </ol>
以上就是PHP截取utf-8字符串的相关方法总结,希望对大家有所帮助。