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 );
}