The difference between strlen and Mb_strlen in PHP

Source: Internet
Author: User
In PHPIn strlenAnd Mb_strlenTwo functions are used to find the length of the string, but also in our daily work is often used in one of the functions, although not difficult to understand, but for some beginners, if you do not read the manual, it may be unclear how they differ.

OK, no nonsense, let's give you an example to explain the difference between the two.

 
  ';//14echo mb_strlen ($str, ' UTF8 '). '
';//6echo mb_strlen ($str, ' GBK '). '
';//8echo mb_strlen ($str, ' gb2312 '). '
';//10?>

Through the analysis of the results, we can see that in the strlen calculation, the encoding format for the UTF8 character is 3 length, so "Chinese a character 1 characters" length is 3*4+2=14, and in the Mb_strlen calculation, the selected inner code is UTF8, A Chinese character will be counted as the length, so the "1 character" length is 6.

The two functions can be combined to calculate the number of placeholders for a mixed Chinese-English string (a placeholder for a Chinese character is 2, the English character is 1)

Echo (strlen ($STR) + Mb_strlen ($str, ' UTF8 '))/2;

For example:

The strlen ($STR) value of "Chinese a character 1" is 14,

The Mb_strlen ($STR) value is 6,

It can be calculated that the placeholder for the "Chinese-a character 1" is 10.

Echo mb_internal_encoding ();

PHP built-in string length function strlen is not able to handle the Chinese string correctly, it can get only the number of bytes of the string. And for GB2312 's Chinese code, strlen get the value is twice times the number of Chinese characters, and then see 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.

Note: Mb_strlen is not the core function of PHP, you must ensure that the Php_mbstring.dll is loaded in php.ini before use, that is, to ensure that the "Extension=php_mbstring.dll" line exists and is not commented out. Otherwise, there is an issue with undefined functions. You can't use this function anymore. Includes functions that start with mb_, such as the MB_SUBSTR () function, and so on.

  • 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.