Example of the difference between string length functions strlen and Mb_strlen, strlenmb_strlen_php tutorial

Source: Internet
Author: User

The example of the difference between the string length function strlen and Mb_strlen is presented, Strlenmb_strlen


The usual functions for calculating string lengths in PHP are: strlen and Mb_strlen. When the word embox is an English character, the two are the same. Here the main comparison, the Chinese and English mixed in the time, two calculation results.

In PHP, strlen and Mb_strlen are functions that seek the length of a string, but for some beginners it may not be clear what the difference is if you don't read the manual.
The difference between the two is explained in the following example.

First look at the example:

<?php//Test file encoding method if UTF8 $str = ' Chinese a word 1 characters '; echo strlen ($str). '
';//14 Echo Mb_strlen ($str, ' UTF8 '). '
';//6 Echo Mb_strlen ($str, ' GBK '). '
';//8 Echo Mb_strlen ($str, ' gb2312 '). '
';//10?>

Results analysis: In the strlen calculation, the treatment of a UTF8 character is 3 length, so "Chinese a character 1 characters" length is 3*4+2=14, in the Mb_strlen calculation, the selected inner code is UTF8, will be a Chinese character as the length of the calculation, so "a character 1 characters" The 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 the Chinese a character 1 character is 14,mb_strlen ($STR) value is 6, then you can calculate the placeholder for the "Chinese-a-word 1-character" is 10.

Echo mb_internal_encoding ();

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.

It is important to note that Mb_strlen is not a PHP core function and needs to be sure that the Php_mbstring.dll is loaded in the php.ini before use, that is, to ensure that the "Extension=php_mbstring.dll" line is present and not commented out. Otherwise, there is an issue with undefined functions.


The strlen function measures the length of the string to include the end identifier

The strlen function does not include the length of ' s ', and the results of sizeof calculation include the length of ': '.
Char str[] = "This is a test string";
cout<< "str sizeof is:" <<><>
cout<< "Str strlen is:" <<><>

Output
STR sizeof IS:26
STR strlen is:25

Look at the example above to make it clearer!

What is the role of strlen () and Mb_strlen, respectively?

In PHP, strlen and Mb_strlen are functions that seek the length of a string, but for some beginners it may not be clear what the difference is if you don't read the manual.
The difference between the two is explained in the following example.
First look at the example:
';//14 Echo Mb_strlen ($str, ' UTF8 '). '
';//6 Echo Mb_strlen ($str, ' GBK '). '
';//8 Echo Mb_strlen ($str, ' gb2312 '). '
';//10?>

Results analysis: In the strlen calculation, the treatment of a UTF8 character is 3 length, so "Chinese a character 1 characters" length is 3*4+2=14, in the Mb_strlen calculation, the selected inner code is UTF8, will be a Chinese character as the length of the calculation, so "a character 1 characters" The 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 the Chinese a character 1 character is 14,mb_strlen ($STR) value is 6, then you can calculate the placeholder for the "Chinese-a-word 1-character" is 10.

Echo mb_internal_encoding ();

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.
It is important to note that Mb_strlen is not a PHP core function and needs to be sure that the Php_mbstring.dll is loaded in the php.ini before use, that is, to ensure that the "Extension=php_mbstring.dll" line is present and not commented out. Otherwise, there is an issue with undefined functions.

http://www.bkjia.com/PHPjc/876529.html www.bkjia.com true http://www.bkjia.com/PHPjc/876529.html techarticle examples of the differences between string length functions strlen and Mb_strlen show that the functions commonly used to calculate string lengths in PHP Strlenmb_strlen are: strlen and Mb_strlen. When the word embox is English character ...

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