Home > Backend Development > PHP Tutorial > Introduction to the specific method of intercepting UTF-8 strings with PHP_PHP tutorial

Introduction to the specific method of intercepting UTF-8 strings with PHP_PHP tutorial

WBOY
Release: 2016-07-15 13:30:34
Original
786 people have browsed it

We For the principle of utf-8, please see UTF-8 FAQ

UTF-8 encoded characters may consist of 1~3 bytes, the specific number It can be judged from the first byte. (Theoretically it may be longer, but here it is assumed that it does not exceed 3 bytes)
The first byte is greater than 224, it and the 2 bytes after it form a UTF-8 character
The first If a byte is greater than 192 and less than 224, it and the 1 byte after it form a UTF-8 character
. Otherwise, the first byte itself is an English character (including numbers and a small part of punctuation marks).

Code previously designed for a website (also the length interception function now used on the homepage)

Code example for PHP interception of utf-8 strings:

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><</span><span> ?php // Cut_Str;  </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>=&rdquo;;  </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><span>$cutlength) and ($i</span><span class="tag"><</span><span>=$str_length))  </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></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><span>=90) <br />//如果是大写字母,  </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; //小写字母和半角标点等与半个高位字符宽&hellip;  </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>
Copy after login

The above is a summary of the relevant methods of intercepting UTF-8 strings in PHP. I hope it will be helpful to everyone.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446272.htmlTechArticleFor our explanation of the principles of utf-8, please see UTF-8 FAQ UTF-8 encoded characters may be from 1~ It consists of 3 bytes. The specific number can be judged from the first byte. (Theoretically it could be longer, but here...
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