php進行GB2312與UTF8編碼轉換的代碼

來源:互聯網
上載者:User
  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. }

  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. 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="123abc中國456def測試正確";
  8. $obj->Convert();
  9. ImageTTFText($im, 20, 0, 5, 50, $white, "SIMKAI.TTF", $obj->utf8);
  10. ImagePNG($im);
  11. ImageDestroy($im);
  12. ?>
複製代碼

註解:需要正確設定font檔案,請先確認是否能夠使用font直接(不使用gb2utf8)輸出英文。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.