Php code conversion implements gbk encoding to utf8

Source: Internet
Author: User
Php code conversion implements gbk encoding to utf8

  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 conversion
  6. Var $ errormsg; // error message during conversion

  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 the gb2312code file to 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 School bbs.it-home.org)

  26. Function convert ()

  27. {// Convert the gb2312 string to the utf8 string. you must set $ gb 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, 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. ?>

Test example:

  1. // Php code conversion
  2. Header ("content-type: image/png ");
  3. $ Im = imagecreate (400,300 );
  4. $ Black = imagecolorallocate ($ im, 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. ?>

Code description: you need to correctly set the font file. make sure that you can use font directly (without 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.