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);
- }
- }//(Scripting Academy bbs.it-home.org)
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;
- }
- }
- ?>
Copy CodeTest examples:
- PHP Code Conversion
- 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);
- ?>
Copy CodeCode Description: The font file needs to be set correctly, please make sure you can use font directly (do not use Gb2utf8) to output English. |