PHP code conversion for GBK encoding conversion to UTF8

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. }//(Scripting Academy bbs.it-home.org)

  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
  40. $this->UTF8.=CHR (substr ($tmp, $i, 3));
  41. }
  42. Else
  43. {
  44. $tmp =substr ($str, 0, 1);
  45. $str =substr ($str, 1,strlen ($STR));
  46. $this->utf8.= $tmp;
  47. }
  48. }
  49. return $this->utf8;
  50. }

  51. function U2utf8 ($INSTR)

  52. {
  53. for ($i =0; $i
  54. $str = "";
  55. if ($instr < 0x80) {
  56. $str. =ord ($INSTR);
  57. }
  58. else if ($instr < 0x800) {
  59. $str. = (0xc0 | $instr >>6);
  60. $str. = (0x80 | $instr & 0x3f);
  61. }
  62. else if ($instr < 0x10000) {
  63. $str. = (0xe0 | $instr >>12);
  64. $str. = (0x80 | $instr >>6 & 0x3f);
  65. $str. = (0x80 | $instr & 0x3f);
  66. }
  67. else if ($instr < 0x200000) {
  68. $str. = (0xf0 | $instr >>18);
  69. $str. = (0x80 | $instr >>12 & 0x3f);
  70. $str. = (0x80 | $instr >>6 & 0x3f);
  71. $str. = (0x80 | $instr & 0x3f);
  72. }
  73. return $str;
  74. }
  75. }
  76. ?>

Copy Code

Test examples:

    1. PHP Code 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 correct";
    9. $obj->convert ();
    10. Imagettftext ($im, 0, 5, $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 make sure you can use 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.