Home >Backend Development >PHP Tutorial >PHP encoding conversion realizes gbk encoding conversion to utf8

PHP encoding conversion realizes gbk encoding conversion to utf8

WBOY
WBOYOriginal
2016-07-25 08:53:491487browse
  1. class gb2utf8

  2. {
  3. var $gb; // The gb2312 string to be converted
  4. var $utf8; // The 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 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. } //(script Xuetangbbs.it-home.org)

  26. function convert()

  27. { // Convert gb2312 string to utf8 string, $gb needs to be set in advance
  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 code

Test example:

  1. //php encoding conversion
  2. header("content-type: image/png");
  3. $im = imagecreate(400,300);
  4. $black = imagecolorallocate($im, 0, 0,0);
  5. $white = imagecolorallocate($im, 184,44,6);
  6. include("gb2utf8.php");
  7. $obj=new gb2utf8();
  8. $obj->gb="123abc China 456def test is correct";
  9. $obj->convert();
  10. imagettftext($im, 20, 0, 5, 50, $white, "simkai.ttf", $obj->utf8);
  11. imagepng( $im);
  12. imagedestroy($im);
  13. ?>
Copy code

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



Statement:
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