convert(); } function setgb2312($instr="gb2312.txt") { // 设置">
当前位置:Gxlcms > PHP教程 > php编码转换实现gbk编码转换为utf8

php编码转换实现gbk编码转换为utf8

时间:2021-07-01 10:21:17 帮助过:37人阅读

  1. class gb2utf8

  2. {
  3. var $gb; // 待转换的gb2312字符串
  4. var $utf8; // 转换后的utf8字符串
  5. var $codetable; // 转换过程中使用的gb2312代码文件数组
  6. var $errormsg; // 转换过程之中的错误讯息

  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. { // 设置gb2312代码文件,默认为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. } //(脚本学堂 bbs.it-home.org)

  26. function convert()

  27. { // 转换gb2312字符串到utf8字符串,需预先设置$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. ?>

测试例子:

  1. //php编码转换
  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中国456def测试正确";
  9. $obj->convert();
  10. imagettftext($im, 20, 0, 5, 50, $white, "simkai.ttf", $obj->utf8);
  11. imagepng($im);
  12. imagedestroy($im);
  13. ?>

代码说明: 需要正确设置font文件,请先确认可以使用font直接(不使用gb2utf8)输出英文。

人气教程排行