Convert GB encoding to UTF8 using PHP

Source: Internet
Author: User

The gb2utf8. php file is as follows:
Copy codeThe Code is as follows:
<? Php
Class GB2UTF8
{
Var $ gb; // The GB2312 string to be converted
Var $ utf8; // The converted UTF8 string
Var $ CodeTable; // array of GB2312 code files used during conversion
Var $ ErrorMsg; // error message during conversion

Function GB2UTF8 ($ InStr = "")
{
$ This-> gb = $ InStr;
$ This-> SetGb2312 ();
($ This-> gb = "")? 0: $ this-> Convert ();
}

Function SetGb2312 ($ InStr = "gb2312.txt ")
{// Set the gb2312code file to gb2312.txt
$ This-> ErrorMsg = "";
$ Tmp = @ file ($ InStr );
If (! $ Tmp ){
$ This-> ErrorMsg = "No GB2312 ";
Return false;
}
$ This-> CodeTable = array ();
While (list ($ key, $ value) = each ($ tmp )){
$ This-> CodeTable [hexdec (substr ($ value, 0, 6)] = substr ($ value, 7, 6 );
}
}

Function Convert ()
{// Convert the GB2312 string to the UTF8 string. You must set $ gb in advance.
$ This-> utf8 = "";
If (! Trim ($ this-> gb) | $ this-> ErrorMsg! = ""){
Return ($ this-> utf8 = $ this-> ErrorMsg );
}
$ Str = $ this-> gb;

While ($ str ){
If (ord (substr ($ str, 127)>)
{
$ Tmp = substr ($ str, 0, 2 );
$ Str = substr ($ str, 2, strlen ($ str ));
$ Tmp = $ this-> U2UTF8 (hexdec ($ this-> CodeTable [hexdec (bin2hex ($ tmp)-0x8080]);
For ($ I = 0; $ I <strlen ($ tmp); $ I + = 3)
$ This-> utf8. = chr (substr ($ tmp, $ I, 3 ));
}
Else
{
$ Tmp = substr ($ str, 0, 1 );
$ Str = substr ($ str, 1, strlen ($ str ));
$ This-> utf8. = $ tmp;
}
}
Return $ this-> utf8;
}

Function U2UTF8 ($ InStr)
{
For ($ I = 0; $ I <count ($ InStr); $ I ++)
$ Str = "";
If ($ InStr <0x80 ){
$ Str. = ord ($ InStr );
}
Else if ($ InStr <0x800 ){
$ Str. = (0xC0 | $ InStr> 6 );
$ Str. = (0x80 | $ InStr & 0x3F );
}
Else if ($ InStr <0x10000 ){
$ Str. = (0xE0 | $ InStr> 12 );
$ Str. = (0x80 | $ InStr> 6 & 0x3F );
$ Str. = (0x80 | $ InStr & 0x3F );
}
Else if ($ InStr <0x200000 ){
$ Str. = (0xF0 | $ InStr> 18 );
$ Str. = (0x80 | $ InStr> 12 & 0x3F );
$ Str. = (0x80 | $ InStr> 6 & 0x3F );
$ Str. = (0x80 | $ InStr & 0x3F );
}
Return $ str;
}
}
?>

The test file is as follows:
Copy codeThe Code is as follows:
<? Php
Header ("Content-type: image/png ");
$ Im = imagecreate (400,300 );
$ Black = ImageColorAllocate ($ im, 0, 0 );
$ White = ImageColorAllocate ($ im, 184,44, 6 );
Include ("gb2utf8. php ");
$ Obj = new gb2utf8 ();
$ Obj-> gb = "123abc China 456def test is correct ";
$ Obj-> Convert ();
ImageTTFText ($ im, 20, 0, 5, 50, $ white, "SIMKAI. TTF", $ obj-> utf8 );
ImagePNG ($ im );
ImageDestroy ($ im );
?>


Note:
To correctly set the font file, make sure that you can use font directly (without gb2utf8) to output English letters.

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.