Home > Backend Development > PHP Tutorial > PHP code for GB2312 and UTF8 encoding conversion

PHP code for GB2312 and UTF8 encoding conversion

WBOY
Release: 2016-07-25 09:08:07
Original
1006 people have browsed it
  1. Class GB2UTF8

  2. {
  3. var $gb; // GB2312 string to be converted
  4. var $utf8; // Converted UTF8 string
  5. var $CodeTable; // Array of GB2312 code files used during the conversion process
  6. var $ErrorMsg; // Error message during the conversion process

  7. function GB2UTF8($InStr="")

  8. {
  9. $this- >gb=$InStr;
  10. $this->SetGb2312();
  11. ($this->gb=="")?0:$this->Convert();
  12. }

  13. function SetGb2312($InStr="gb2312.txt")

  14. { // Set the gb2312 code file, the default is gb2312.txt
  15. $this->ErrorMsg="";
  16. $tmp=@file($InStr );
  17. if (!$tmp) {
  18. $this->ErrorMsg="No GB2312";
  19. return false;
  20. }
  21. $this->CodeTable=array();
  22. while(list($key,$ value)=each($tmp)) {
  23. $this->CodeTable[hexdec(substr($value,0,6))]=substr($value,7,6);
  24. }
  25. }
  26. function Convert()

  27. { // To convert GB2312 string to UTF8 string, you need to set $gb
  28. $this->utf8="";
  29. if(!trim($this-> gb) || $this->ErrorMsg!="") {
  30. return ($this->utf8=$this->ErrorMsg);
  31. }
  32. $str=$this->gb;
  33. while($str) {

  34. if (ord(substr($str,0,1))>127)
  35. {
  36. $tmp=substr($str,0,2);
  37. $ str=substr($str,2,strlen($str));
  38. $tmp=$this->U2UTF8(hexdec($this->CodeTable[hexdec(bin2hex($tmp))-0x8080]));
  39. for($i=0;$i$this->utf8.=chr(substr($tmp,$i,3));
  40. }
  41. else
  42. {
  43. $tmp=substr($str,0,1);
  44. $str=substr($str,1,strlen($str));
  45. $this->utf8.=$tmp;
  46. }
  47. }
  48. return $this->utf8;
  49. }

  50. function U2UTF8($InStr)

  51. {
  52. for($i=0;$i$ str="";
  53. if ($InStr < 0x80) {
  54. $str.=ord($InStr);
  55. }
  56. else if ($InStr < 0x800) {
  57. $str.=(0xC0 | $InStr> >6);
  58. $str.=(0x80 | $InStr & 0x3F);
  59. }
  60. else if ($InStr < 0x10000) {
  61. $str.=(0xE0 | $InStr>>12);
  62. $ str.=(0x80 | $InStr>>6 & 0x3F);
  63. $str.=(0x80 | $InStr & 0x3F);
  64. }
  65. else if ($InStr < 0x200000) {
  66. $str.=(0xF0 | $InStr>>18);
  67. $str.=(0x80 | $InStr>>12 & 0x3F);
  68. $str.=(0x80 | $InStr>>6 & 0x3F);
  69. $str.= (0x80 | $InStr & 0x3F);
  70. }
  71. return $str;
  72. }
  73. }
  74. ?>

Copy the code

Test it and see the effect:

  1. Header("Content-type: image/png");
  2. $im = imagecreate(400,300);
  3. $black = ImageColorAllocate($im, 0,0,0);
  4. $white = ImageColorAllocate($im, 184,44,6);
  5. include("gb2utf8.php");
  6. $obj=new gb2utf8();
  7. $obj->gb="123abcChina 456def test is correct";
  8. $obj->Convert();
  9. ImageTTFText($im, 20, 0, 5, 50, $white, "SIMKAI.TTF", $obj->utf8);
  10. ImagePNG($im);
  11. ImageDestroy ($im);
  12. ?>
Copy code

Notes: The font file needs to be set correctly. Please first confirm whether you can use font to output English directly (without using gb2utf8).



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template