Questions about mb_strlen

Source: Internet
Author: User
Newbie's question about mb_strlen. I don't quite understand the returned values of these three calls .. PHPcode & lt ;? Phpechomb_strlen (& quot; my name & quot;). & quot; & lt; br/& gt; & quot; // Questions about mb_strlen for beginners of 12echomb_str.
I don't quite understand the returned values of these three calls ..

PHP code
  
  "; // 12 echo mb_strlen (" My Name "," UTF-8 ")."
"; // 4 echo mb_strlen (" My Name "," GBK ") // 6?>


The code is saved in l. php, and the file is encoded as utf8

About the second encoding parameter of mb_strlen. Written in the PHP manual:

"The encoding parameter is the character encoding. If it is omitted, the internal character encoding value will be used ."
I want to know which encoding the internal character encoding refers to and where it is configured.

Also, the return value of this function is written in this way.
"Returns the number of characters in string str having character encoding. A multi-byte character is counted as 1 ."
Multi-byte characters are calculated by 1.
The first call will return 12, and the second call will return 6 .. I don't quite understand. I hope to explain it to you. thank you!

------ Solution --------------------
Discussion
Well, there is another small problem. If you want to use functions in other DLL files in PHP, do you want to copy the DLL to system32, and then add it to php. ini?
Can Code Like extension = php_fdf.dll be used?

------ Solution --------------------
PHP code
Echo mb_strlen ("My Name ")."
"; // 12 echo mb_strlen (" My Name "," UTF-8 ")."
"; // 4 echo mb_strlen (" My Name "," GBK ") // 6
------ Solution --------------------
First your file encoding must be UTF-8, and your mb_internal_encoding is similar to the ISO-8859-1
Then we need to know that one utf8 Chinese character is 3 bytes, and gb is 2 bytes. mb_strlen counts the multi-byte word as 1, so we will get
Echo mb_strlen ("My Name ")."
"; // Unspecified encoding, calculated by default ISO-8859-1, a utf8 Chinese character is 3
Echo mb_strlen ("My Name", "UTF-8 ")."
"; // Given the correct encoding, one Chinese character is 1
Echo mb_strlen ("My Name", "GBK") // given the error code, GBK calculates 2 for a Chinese character, but your file is utf8, 4*3 = 12 bytes/2 = 6 gbk characters
?>

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.