A section of PHP code that converts GB encoding to UTF8

Source: Internet
Author: User
Tags ord php code strlen utf 8

A section of code that converts GB encoding to UTF8
gb2utf8.php files are as follows:
Class Gb2utf8
{
var $gb; GB2312 string to be converted
var $utf 8; Converted UTF8 String
var $CodeTable; Array of GB2312 code files used during conversion
var $ErrorMsg; Error messages during the conversion process

function Gb2utf8 ($InStr = "")
{
$this->gb= $InStr;
$this->setgb2312 ();
($this->gb== "")? 0: $this->convert ();
}

function SetGb2312 ($InStr = "Gb2312.txt")
{//Set gb2312 code file, defaults 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 GB2312 string to UTF8 string, required to be set in advance $GB
$this->utf8= "";
if (!trim ($this->gb) | | $this->errormsg!= "") {
Return ($this->utf8= $this->errormsg);
}
$str = $this->gb;

while ($STR) {
if (Ord (substr ($STR, 0,1)) >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-&GT;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 files are as follows:
Php
Header ("Content-type:image/png");
$im = Imagecreate (400,300);
$black = Imagecolorallocate ($im, 0,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, 0, 5, $white, "Simkai. TTF ", $obj->utf8);
Imagepng ($im);
Imagedestroy ($im);
Description
The font file needs to be set correctly, please confirm that you can use font directly (without Gb2utf8) to output English.
Gb2312.txt Please search the Internet

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.