The gb2utf8.php file is as follows:
Copy the Code code as follows:
Class Gb2utf8
{
var $gb; GB2312 string to convert
var $utf 8; Converted UTF8 String
var $CodeTable; An array of GB2312 code files used in the conversion process
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, default is 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, pre-set $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 $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
$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:
Copy the Code code as follows:
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 correct";
$obj->convert ();
Imagettftext ($im, 0, 5, $white, "Simkai. TTF ", $obj->utf8);
Imagepng ($im);
Imagedestroy ($im);
?>
Description
To set up the font file correctly, make sure you can use the font directly (without using GB2UTF8) to output English.
The above describes the Unicode encoding conversion using PHP to convert GB encoding to UTF8, including the content of Unicode encoding conversion, I hope to be interested in PHP tutorial friends helpful.