PHP (1) --- process Chinese character strings in php: strlen and mb

Source: Internet
Author: User
I officially started learning PHP today, but I accidentally discovered something that requires special attention when using PHP. For example: htmlmetacharsetutf-8body? Php $ name; print name:. strlen ($ name );? The output result of the bodyhtml code is: The name length is: 6.

I officially started learning PHP today, but I accidentally discovered something that requires special attention when using PHP. Example: htmlmeta charset = utf-8body? Php $ name = Naruto; print name Length:. strlen ($ name );? The output result of the/body/html code is: The name length is 6.


I officially started learning PHP today, but I accidentally discovered something that requires special attention when using PHP.

For example:

 
 LengthIs: ". strlen ($ name);?>

The output result of this Code is: Name Length6

First, let's discuss why this problem occurs: PHP built-inStringLengthThe strlen () function is incorrect.ProcessingChineseString, It only obtainsStringThe number of bytes occupied. For GB2312 Chinese encoding, The strlen value isChinese charactersThe number of 2 times, and for the UTF-8 encoding of Chinese, is 3 times the difference (in the UTF-8 encoding,Chinese characters3 bytes ).


If you want to use the strlen function to calculateStringOfLengthYes, too. The following example is taken online:

 0xa0) {             $tmpstr.=substr($str,$i,2);             $i++;         } else             $tmpstr.=substr($str,$i,1);     }     return $tmpstr; }?>

However, apart from this, we can use the mb_strlen function to calculate:
 
 Length". Mb_strlen ($ name," UTF-8 ");?>

The output result is: NameLength2


The usage of mb_strlen is similar to that of strlen, except that it has a second optional parameter for specifying character encoding.

It should be noted that mb_strlen is not a PHP core function. before using it, make sure that. the "php_mbstring.dll" line is loaded in ini to ensure that the "extension = php_mbstring.dll" line exists and is not commented out. Otherwise, the problem of undefined functions may occur.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.