Learning Points
strlen get Chinese length
with mb_strlen
<?php
$name="鸣人";
print "姓名的长度为:".strlen($name);
?>
Is output: names of length: 6
To explore why there is this problem:
PHP built-in string length function strlen () does not properly handle Chinese string, the string number of bytes occupied it gets only.
For the Chinese GB2312 encoding, strlen obtained value is 2 times the number of characters,
for the UTF-8 encoding of the Chinese, is a 3-fold difference (in UTF-8 encoding, a character 3 bytes)
With mb_strlen
<?php
$name="鸣人";
print "姓名的长度为:".mb_strlen($name,"utf-8");
?>
Names of length: 2
mb_strlen usage and strlen similar, except that it has a second optional parameter specifies the character encoding.
Note that, mb_strlen not the PHP core function, need to make sure before using php_mbstring.dll loaded in php.ini, ensuring that there is "extension = php_mbstring.dll" and this line is not commented out, otherwise there will be undefined problem function.