PHP automatically identifies character set encoding and completes transcoding

Source: Internet
Author: User
Tags character set ord

When PHP processes the page, we use iconv or mb_convert functions for character set Conversion. However, this is actually a prerequisite. That is, we must know in and out encoding in advance 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 and GB2312 are supported, it is enough for most websites in China.

The code is as follows: Copy code


Function safeEncoding ($ string, $ outEncoding = 'utf-8 ')
{
$ Encoding = "UTF-8 ";
For ($ I = 0; $ I <128)
Continue;

If (ord ($ string {$ I}) & 224) = 224)
        {
// The first byte is determined
$ Char = $ string {++ $ I };
If (ord ($ char) & 128) = 128)
            {
// The second byte is passed
$ Char = $ string {++ $ I };
If (ord ($ char) & 128) = 128)
                {
$ Encoding = "UTF-8 ";
Break;
                }
            }
        }
If (ord ($ string {$ I}) & 192) = 192)
        {
// The first byte is determined
$ Char = $ string {++ $ I };
If (ord ($ char) & 128) = 128)
            {
// The second byte is passed
$ Encoding = "GB2312 ";
Break;
            }
        }
    }

If (strtoupper ($ encoding) = strtoupper ($ outEncoding ))
Return $ string;
Else
Return iconv ($ encoding, $ outEncoding, $ string );
}

Example 2

The code is as follows: Copy code


// Identify the Chinese character encoding. YBlog uses UTF-8. If the gb2312 encoding is referenced in the announcement, the encoding must be recognized and converted.
Function safeEncoding ($ string, $ outEncoding = 'utf-8 ')
    { 
$ Encoding = "UTF-8 ";
For ($ I = 0; $ I <strlen ($ string); $ I ++)
        { 
If (ord ($ string {$ I}) <128)
Continue;
    
If (ord ($ string {$ I}) & 224) = 224)
            { 
// The first byte is determined
$ Char = $ string {++ $ I };
If (ord ($ char) & 128) = 128)
                { 
// The second byte is passed
$ Char = $ string {++ $ I };
If (ord ($ char) & 128) = 128)
                    { 
$ Encoding = "UTF-8 ";
Break;
                    } 
                } 
            } 
If (ord ($ string {$ I}) & 192) = 192)
            { 
// The first byte is determined
$ Char = $ string {++ $ I };
If (ord ($ char) & 128) = 128)
                { 
// The second byte is passed
$ Encoding = "GB2312 ";
Break;
                } 
            } 
        } 
         
If (strtoupper ($ encoding) = strtoupper ($ outEncoding ))
Return $ string;
Else
Return iconv ($ encoding, $ outEncoding, $ string );
    }

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