Without iconv function to implement UTF-8 encoding conversion GB2312 PHP function-PHP source code

Source: Internet
Author: User
Ec (2); if I use the iconv () function to convert and encode the code, it is relatively simple, but many virtual hosts do not support this component. I am searching online for half a day, to find a method for converting gb2312 to UTF-8, but cannot reverse conversion. This function is as follows: ***** * *********************** functiongb2utf8 script ec (2 ); script

If iconv () function conversion encoding is relatively simple, but many virtual hosts do not support this component, I found a method to convert gb2312 to UTF-8 after searching for half a day on the Internet, but cannot reverse conversion.

This function is as follows:

/*******************************
// GB to UTF-8 Encoding
*******************************/
Function gb2utf8 ($ gbstr ){
Global $ CODETABLE;
If (trim ($ gbstr) = "") return $ gbstr;
If (empty ($ CODETABLE )){
$ Filename = dirname (_ FILE _). "/gb2312-utf8.table ";
$ Fp = fopen ($ filename, "r ");
While ($ l = fgets ($ fp, 15 ))
{$ CODETABLE [hexdec (substr ($ l, 0, 6)] = substr ($ l, 7, 6 );}
Fclose ($ fp );
}
$ Ret = "";
$ Utf8 = "";
While ($ gbstr ){
If (ord (substr ($ gbstr, 0, 1) & gt; 127 ){
$ ThisW = substr ($ gbstr, 0, 2 );
$ Gbstr = substr ($ gbstr, 2, strlen ($ gbstr ));
$ Utf8 = "";
@ $ Utf8 = u2utf8 (hexdec ($ CODETABLE [hexdec (bin2hex ($ thisW)-0x8080]);
If ($ utf8! = ""){
For ($ I = 0; $ I <strlen ($ utf8); $ I = 3)
$ Ret. = chr (substr ($ utf8, $ I, 3 ));
}
}
Else
{
$ Ret. = substr ($ gbstr, 0, 1 );
$ Gbstr = substr ($ gbstr, 1, strlen ($ gbstr ));
}
}
Return $ ret;
}
// Unicode to utf8
Function u2utf8 ($ c ){
For ($ I = 0; $ I <count ($ c); $ I)
$ Str = "";
If ($ c <0x80 ){
$ Str. = $ c;
} Else if ($ c <0x800 ){
$ Str. = (0xC0 | $ c> 6 );
$ Str. = (0x80 | $ c & 0x3F );
} Else if ($ c <0x10000 ){
$ Str. = (0xE0 | $ c> 12 );
$ Str. = (0x80 | $ c> 6 & 0x3F );
$ Str. = (0x80 | $ c & 0x3F );
} Else if ($ c <0x200000 ){
$ Str. = (0xF0 | $ c> 18 );
$ Str. = (0x80 | $ c> 12 & 0x3F );
$ Str. = (0x80 | $ c> 6 & 0x3F );
$ Str. = (0x80 | $ c & 0x3F );
}

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.