Dynamic conversion from GB encoding to UTF-8 encoding using PHP

Source: Internet
Author: User
Using the ip2addr function to directly read IP database files is the most efficient. compared with using the MySQL database to store IP data, using SQL queries is the lowest efficiency. However, the IP database file QQWry. dat is GB2312 encoded. Now I need the geographic status results of UTF-8 encoding.

Using the ip2addr function to directly read IP database files is the most efficient. compared with using the MySQL database to store IP data, using SQL queries is the lowest efficiency. However, the IP database file QQWry. dat is GB2312 encoded. Now I need the geographic status results of UTF-8 encoding. If MySQL method can be used, the data can be converted to UTF-8 encoding when stored in the database, once and for all. However, the QQWry. dat file cannot be modified, and the output of the ip2addr function can only be converted dynamically.

There are at least four methods for dynamic conversion of GB-> UTF-8 encoding:

Use PHP's iconv to expand the conversion

Extended conversion using PHP mb_string

Use swap table conversion, swap table stored in MySQL database

Convert tables with swap tables and store them in text files.

The first two methods require the server to make corresponding settings (compilation and installation of the corresponding expansion) for the application. My VM does not have these two extensions, so I have to consider the last two methods. The first two methods are not evaluated in this document.

The evaluation procedure is as follows (for func_ip.php, see the article "IP address-> geographic status conversion evaluation ):

Require_once ('func_ip. php ');
Function u2utf8 ($ c ){
$ Str = '';
If ($ c <0x80 ){
$ Str. = $ c;
} Elseif ($ c <0x800 ){
$ Str. = chr (0xC0 | $ c> 6 );
$ Str. = chr (0x80 | $ c & 0x3F );
} Elseif ($ c <0x10000 ){
$ Str. = chr (0xE0 | $ c> 12 );
$ Str. = chr (0x80 | $ c> 6 & 0x3F );
$ Str. = chr (0x80 | $ c & 0x3F );
} Elseif ($ c <0x200000 ){
$ Str. = chr (0xF0 | $ c> 18 );
$ Str. = chr (0x80 | $ c> 12 & 0x3F );
$ Str. = chr (0x80 | $ c> 6 & 0x3F );
$ Str. = chr (0x80 | $ c & 0x3F );
}
Return $ str;
}
Function GB2UTF8_ SQL ($ strGB ){
If (! Trim ($ strGB) return $ strGB;
$ StrRet = '';
$ IntLen = strlen ($ strGB );
For ($ I = 0; $ I <$ intLen; $ I ){
If (ord ($ strGB {$ I})> 127 ){
$ StrCurr = substr ($ strGB, $ I, 2 );
$ IntGB = hexdec (bin2hex ($ strCurr)-0x8080;
$ StrSql = 'SELECT code_unicode FROM nnstats_gb_unicode
WHERE code_gb = '. $ intGB. 'limit 1'
;
$ ResResult = mysql_query ($ strSql );
If ($ arrCode = mysql_fetch_array ($ resResult) $ strRet. = u2utf8 ($ arrCode ['code _ unicode ']);
Else $ strRet. = '?? ';
$ I;
} Else {
$ StrRet. = $ strGB {$ I };
}
}
Return $ strRet;
}
Function GB2UTF8_FILE ($ strGB ){
If (! Trim ($ strGB) return $ strGB;
$ ArrLines = file('gb_unicode.txt ');
Foreach ($ arrLines as $ strLine ){
$ ArrCodeTable [hexdec (substr ($ strLine, 0, 6)] = hexdec (substr ($ strLine, 7, 6 ));

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.