Detailed PHP strlen and MB

Source: Internet
Author: User
Tags manual strlen

  in PHP, strlen and Mb_strlen are functions that ask for string lengths, but for some beginners, it may not be clear what the difference is if you don't read the manual.

In PHP there are two functions that compute the number of strings one is strlen, the other is Mb_strlen; Let's take a look at the definition in the manual. Strlen strlen-Gets the string length int strlen (string $string) returns the length of the given string.   Mb_strlen int Mb_strlen (String $str [, String $encoding]) returns the length of a given string of strings. The encoding parameter is a character encoding. If omitted, the internal character encoding is used.   So in addition to Mb_strlen can pass a character encoding seems to have no other difference, the following examples, explain the difference between the two. First look at the example:     code as follows: <?php    //test file encoding mode if UTF8     $STR = ' Chinese a word 1 characters ';     Echo strlen ($str). ' <br>//14     echo mb_strlen ($str, ' UTF8 '). ' <br>//6     echo mb_strlen ($str, ' GBK '). ' <br>//8     echo mb_strlen ($str, ' gb2312 '). ' <br>//10    ?>    result analysis: In strlen calculations, the Chinese character for a UTF8 is 3 lengths, so the length of the word "1" is 3*4+2=14 in Mb_ Strlen calculation, when the selected inner code is UTF8, a Chinese character is calculated as a length, so the length of the "1 character" is 6. By using these two functions, you can jointly calculate how much of a Chinese-English mixed-row string is (a Chinese character occupies a position of 2 English characters are 1) copy code code as follows: Echo (strlen ($STR) + Mb_strlen ($str, ' UTF8 '))/2;     For example, "Chinese a character 1" strlen ($STR) value is the 14,mb_strlen ($STR) value is 6, you can calculate the "Chinese a word 1" occupies the position is 10. Copy code code as follows: Echo mb_iNternal_encoding ();    PHP built-in string length function strlen cannot handle the Chinese string correctly, it gets just the number of bytes in the string. For GB2312 Chinese encoding, strlen gets twice times the number of Chinese characters, and for UTF-8 encoded Chinese, it is 3 times times the difference (in UTF-8 code, a Chinese character occupies 3 bytes).   Using Mb_strlen function can solve this problem better. The use of Mb_strlen is similar to strlen, except that it has a second optional parameter for specifying the character encoding. For example, to get the UTF-8 string $str length, you can use Mb_strlen ($str, ' UTF-8 '). If the second argument is omitted, PHP's internal encoding is used. The internal code can be obtained by the mb_internal_encoding () function. It is important to note that Mb_strlen is not a PHP core function, and Windows needs to be sure to load the Php_mbstring.dll in php.ini before it is used, that is, to ensure that "Extension=php_mbstring.dll" This line exists and is not commented out, otherwise there is an issue with undefined functions. Linux needs to compile this extension.
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.