The phpmb_strlen function specifies different encoding output results

Source: Internet
Author: User
The phpmb_strlen function specifies different encoding output results. The mb_strlen function specifies different encoding output results.

Page encoding UTF-8
$ Text = "aha"; echo mb_strlen ($ text, 'utf8 ')."
"; Echo mb_strlen ($ text, 'gbk ')."
"; Echo mb_strlen ($ text, 'gb2312 ')."
"; Echo strlen ($ text );

Output: 4 6 8 12

Page code gb2312
$ Text = "aha"; echo mb_strlen ($ text, 'utf8 ')."
"; Echo mb_strlen ($ text, 'gbk ')."
"; Echo mb_strlen ($ text, 'gb2312 ')."
"; Echo strlen ($ text );

Output: 4 4 4 8


Reply to discussion (solution)

If you specify the wrong encoding, the correct result will not be obtained.

This is the PHP official website Character Set list
Http://www.php.net/manual/en/mbstring.supported-encodings.php


Mb_internal_encoding ("UTF-8 ");
Echo mb_internal_encoding ();

The hexadecimal representation of "ah" is
UTF-8: E5 95 8A E5 95 8A E5 95 8A E5 95 8A --- 12
GB2312: B0 A1 B0 A1 B0 A1 B0 A1 --- 8

In UTF-8
UTF-8 [E5 95 8A] [E5 95 8A] [E5 95 8A] [E5 95 8A] --- 4
Gbk [E5 95]? [8A E5]? [95 8A]? [E5 95]? [8A E5]? [95 8A]? --- 6
Gb2312 [E5 95]? [8A] [E5 95]? [8A] [E5 95]? [8A] [E5 95]? [8A] --- 8
Note: 8A does not start with gb2312 (minimum A1), so it is calculated independently.

At gb2312
UTF-8 (not sure) because there is no UTF-8 character starting with B0 bytes, I guess mb is "intelligently" calculated by double byte --- 4
Gbk/gb2312 [B0 A1] [B0 A1] [B0 A1] [B0 A1] --- 4

Test results (php 5.4.12)
UTF-8: 4 6 8 12
Get 8 4 4 8 under gb2312

No need to explain it. only the correct character set can get the correct result.

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.