PHP code for GB2312 and UTF8 encoding conversion

Source: Internet
Author: User
Tags utf 8
  1. Class Gb2utf8

  2. {
  3. var $gb; GB2312 string to convert
  4. var $utf 8; Converted UTF8 String
  5. var $CodeTable; An array of GB2312 code files used in the conversion process
  6. var $ErrorMsg; Error messages 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, 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. {//Convert GB2312 string to UTF8 string, pre-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<>
  53. $str = "";
  54. if ($InStr < 0x80) {
  55. $str. =ord ($INSTR);
  56. }
  57. else if ($InStr < 0x800) {
  58. $str. = (0xC0 | $InStr >>6);
  59. $str. = (0x80 | $InStr & 0x3F);
  60. }
  61. else if ($InStr < 0x10000) {
  62. $str. = (0xE0 | $InStr >>12);
  63. $str. = (0x80 | $InStr >>6 & 0x3F);
  64. $str. = (0x80 | $InStr & 0x3F);
  65. }
  66. else if ($InStr < 0x200000) {
  67. $str. = (0xF0 | $InStr >>18);
  68. $str. = (0x80 | $InStr >>12 & 0x3F);
  69. $str. = (0x80 | $InStr >>6 & 0x3F);
  70. $str. = (0x80 | $InStr & 0x3F);
  71. }
  72. return $str;
  73. }
  74. }
  75. ?>

Copy Code

Under test, look at 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= "123abc China 456def test correct";
    8. $obj->convert ();
    9. Imagettftext ($im, 0, 5, $white, "Simkai. TTF ", $obj->utf8);
    10. Imagepng ($im);
    11. Imagedestroy ($im);
    12. ?>
Copy Code

Note: The font file needs to be set correctly, first confirm whether you can use the font directly (do not use Gb2utf8) to output English.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.