Home > Article > Backend Development > PHP counts the length of Chinese strings, converts strings to numbers, php splits strings, php string length, ph
Chinese websites generally choose two encodings: gbk/gb2312 or utf-8.
Each Chinese character in gbk encoding occupies 2 bytes, for example:
<code><span>$zhStr</span> = ‘您好,中国!’; <span>echo</span> strlen(<span>$zhStr</span>); <span>// 输出:12</span></code>
<code>utf-8编码下每个中文字符所占字节为3,例: </code>
<code><span>$zhStr</span> = ‘您好,中国!’; <span>echo</span> strlen(<span>$zhStr</span>); <span>// 输出:18</span></code>
So how to calculate the length of this set of Chinese strings? Some people may say that dividing the length of the Chinese string by 2 under gbk, and dividing it by 3 under utf-8 encoding will suffice? But you have to consider that strings are not honest, and 99% of the time they will appear as a mix of Chinese and English.
This is a piece of code in WordPress. The main idea is to first use regular expressions to decompose the string into individual units, and then calculate the number of units, which is the length of the string. The code is as follows (only strings encoded in UTF-8 can be processed) :
<code><span>$zhStr</span> = ‘您好,中国!’; <span>$str</span> = ‘Hello,中国!’; <span>// 计算中文字符串长度</span><span><span>function</span><span>utf8_strlen</span><span>(<span>$string</span> = null)</span> {</span><span>// 将字符串分解为单元</span> preg_match_all(“/./us”, <span>$string</span>, <span>$match</span>); <span>// 返回单元个数</span><span>return</span> count(<span>$match</span>[<span>0</span>]); } <span>echo</span> utf8_strlen(<span>$zhStr</span>); <span>// 输出:6</span><span>echo</span> utf8_strlen(<span>$str</span>); <span>// 输出:9</span></code>
Below I have encapsulated a function to accurately calculate the length of Chinese string:
<code><span><span>function</span><span>count_strlen</span><span>(<span>$string</span> = null)</span> {</span><span>$fileType</span> = mb_detect_encoding(<span>$string</span> , <span>array</span>(<span>'UTF-8'</span>,<span>'GBK'</span>,<span>'LATIN1'</span>,<span>'BIG5'</span>)) ; <span>//判断字符串中文编码的类型</span><span>$length</span> = iconv_strlen(<span>$string</span>,<span>$fileType</span>);<span>//根据字符编码计算字符串长度</span><span>return</span><span>$length</span>; } <span>$str</span> = <span>"中文45汶"</span>; <span>$len</span> = count_strlen(<span>$str</span>); <span>echo</span><span>$len</span>; <span>//输出5</span></code>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });
The above introduces PHP to count the length of Chinese strings, including string and PHP content. I hope it will be helpful to friends who are interested in PHP tutorials.