Home > Backend Development > PHP Tutorial > php获取utf8字符串的字符长度

php获取utf8字符串的字符长度

WBOY
Release: 2016-06-20 13:05:23
Original
1186 people have browsed it

php获取utf8字符串的字符长度,摘自大名鼎鼎的WordPress,非常精确的哦,另外需要注意的是本函数仅适用于utf-8 编码下的字符串。

<span style="font-size: 14px;">function utf8_strlen($string){</span><br />$string=trim($string);<br />if(empty($string)) return 0;<br />    // 将字符串分解为单元<br />    preg_match_all("/./us", $string, $match);<br />    // 返回单元个数   <br />    return count($match[0]);<br /><p>}<span style="font-size: 14px;">
Copy after login

另外对于gbk/gb2312编码下的字符串,直接使用PHP自带的函数如 strlen()、mb_strlen() 就行了。


Related labels:
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