PHP automatically identifies character set encoding and completes transcoding

Source: Internet
Author: User
The principle is very simple, because gb2312gbk is a Chinese byte, the two bytes have a value range, while UTF-8 contains three Chinese characters, and each byte also has a value range, english, regardless of the encoding, is less than 128, takes only one byte, full angle... the principle is very simple, because gb2312/gbk is a Chinese byte, the two bytes have a value range, while the Chinese character in UTF-8 is three bytes, and each byte also has a value range, english, regardless of the encoding, is less than 128, only occupies one byte, except for the full angle.

When PHP processes the page, we use iconv, mb_convert, and other functions for character set conversion, but this is actually a prerequisite, that is, we need to know the encoding of in and out before we can perform the correct conversion.

The following function can automatically determine the source string encoding and convert it without knowing the source string encoding. although only UTF8 encoding and GB2312 encoding are supported, for most websites in China, the code is as follows:

 

Recognize Chinese character encoding, because YBlog uses UTF-8. if the gb2312 encoding is sent from the reference announcement, you need to identify and complete the encoding conversion. the code is as follows:

 

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.