PHP built-in functions strlen and mbstring extension function Mb_strlen differences

Source: Internet
Author: User

#EXAMPLE

$str _uncode = "Simplified Chinese Chinese (Simplified)";//statistical string length echo strlen ($str _uncode). ' <br> ';//Result: 31echo Mb_strlen ($str _uncode, ' UTF-8 '). ' <br> '; Results: 23echo Mb_strlen ($str _uncode, ' GBK '). ' <br> '; Results: 25echo Mb_strlen ($str _uncode, ' GB2312 '). ' <br> '; result 27

  

Results Analysis:

Strlen a Chinese by 3 bytes (complex Chinese characters will be counted as 4 bytes)

Mb_strlen ' UTF-8 ' encodes a Chinese character to be counted by a byte bit

PHP built-in string length function strlen cannot handle Chinese strings correctly, it only gets the number of bytes that the string occupies. For the Chinese encoding of GB2312, strlen get the value is twice times the number of Chinese characters, and for UTF-8 encoded in Chinese, is 3 times times the difference (in UTF-8 encoding, a Chinese character accounted for 3 bytes).

Using Mb_strlen function can solve this problem well. The usage of Mb_strlen is similar to strlen, except that it has a second optional parameter for specifying character encoding. For example get UTF-8 string $str length, can be used Mb_strlen ($str, ' UTF-8 ').

If you omit the second argument, the internal encoding of PHP is used. The internal code can be obtained through the mb_internal_encoding () function.

Mb_strlen is not a PHP core function, you need to make sure to load the Php_mbstring.dll in php.ini, that is, to ensure that the "Extension=php_mbstring.dll" line exists and is not commented out, otherwise there will be undefined letter Number of questions.

PHP built-in functions strlen and mbstring extension function Mb_strlen differences

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.