PHP Kanji transcoding gbk->unicode (UTF8) encoding conversion

Source: Internet
Author: User
  1. PHP Kanji transcoding gbk->unicode (UTF8)
  2. Class qswhgbk{
  3. var $qswhData;
  4. function QSWHGBK ($filename = "qswhgbk.php") {
  5. $this->qswhdata=file ($filename);
  6. }
  7. function gb2u ($GB, $callback = "") {
  8. /****** (Qiushuiwuhen 2002-8-15) ******/
  9. $ret = "";
  10. for ($i =0; $i<>
  11. if ($p =ord (substr ($GB, $i, 1)) >127) {
  12. $q =ord (substr ($GB, + + $i, 1));
  13. $q = ($q-($q >128?65:64))
  14. $q =substr ($this->qswhdata[$p -128], $q, 4);
  15. }
  16. Else
  17. $q =dechex ($p);
  18. if (empty ($callback))
  19. $ret. = $q;
  20. else {
  21. $arr =array ("Htmlhex", "Htmldec", "Escape", "U2utf8");
  22. if (Is_integer ($callback)) {
  23. if ($callback >count ($arr)) Die ("Invalid Function");
  24. $ret. = $this $arr [$callback-1] ($q);
  25. }else
  26. $ret. = $callback ($q);
  27. }
  28. }
  29. return $ret;
  30. }
  31. function Htmlhex ($STR) {
  32. Return "& #x". $str. ";";
  33. }//Programmer's Home bbs.it-home.org
  34. function Htmldec ($STR) {
  35. Return "" ". Hexdec ($STR)."; ";
  36. }
  37. function Escape ($STR) {
  38. Return Hexdec ($STR) <256?chr (Hexdec ($STR)): "%u". $str;
  39. }
  40. function U2utf8 ($STR) {
  41. /****** (Qiushuiwuhen 2002-8-15) ******/
  42. $SP = "! ' () *-.0123456789abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz~ ";
  43. $dec =hexdec ($STR);
  44. $bin =decbin ($DEC);
  45. $len =strlen ($bin);
  46. $arr =array ("C0", "E0", "F0");
  47. if ($dec >0x7f) {
  48. $ret = "";
  49. for ($i = $len, $j =-1; $i >=0; $i-=6, $j + +) {
  50. if ($i >6)
  51. $ret = "%". Dechex (0x80+bindec (substr ($bin, $i -6,6)). $ret;
  52. Else
  53. $ret = "%". Dechex (Hexdec ($arr [$j]) +bindec (substr ($bin, 0,6-$i)). $ret;
  54. }
  55. }else{
  56. if (Strpos ($SP, Chr ($DEC)))
  57. $ret =CHR ($DEC);
  58. Else
  59. $ret = "%". Strtolower ($STR);
  60. }
  61. return $ret;
  62. }
  63. }
Copy Code

Invocation Example:

  1. $words = "Chinese abc";

  2. function Ex ($STR) {return "[". $str. "]";}

  3. $QSWH =new QSWHGBK ("qswhgbk.php");//If the file name is qswhgbk.php, you can save the parameter

  4. Echo ("

    without parameters:". $qswh-&amp;gt;gb2u ($words)); &amp;lt;li&amp;gt;echo ("/n calls built-in function Htmlhex:". $qswh-&amp;gt;gb2u ($words, 1 ); &amp;lt;li&amp;gt;echo ("/n calls built-in function Htmldec:". $qswh-&amp;gt;gb2u ($words, 2)); &amp;lt;li&amp;gt;echo ("/n calls built-in function escape:". $qswh-&amp;gt;gb2u ($words, 3)); &amp;lt;li&amp;gt;echo ("/n calls the built-in function U2utf8:". $qswh-&amp;gt;gb2u ($words, 4)); &amp;lt;li&amp;gt;echo ("/n calls a custom function:". $QSWH-&amp;gt;gb2u ($words, ex)); &amp;lt;/p&amp;gt;&amp;lt;li&amp;gt;&amp;lt;/ol&amp;gt;&amp;lt;em onclick= "Copycode ($ (' code_sd4 '));" &amp;gt; Copy Code &amp;lt;/em&amp;gt;&amp;lt;p&amp;gt; Effects: &amp;lt;/p&amp;gt; without parameters: 4e2d6587416263 calls the built-in function htmlhex:&amp;amp; #x4E2D;&amp;amp; #x6587;&amp;amp; #x41; &amp;amp; #x62;&amp;amp; #x63, call built-in functions htmldec:&amp;amp; #20013;&amp;amp; #25991;&amp;amp; #65;&amp;amp; #98;&amp;amp; #99; call built-in functions escape:%u4e2d% U6587ABC call a built-in function U2UTF8:%E4%B8%AD%E6%96%87ABC call a custom function:[4e2d][6587][41][62][63]&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&amp;lt;/table&amp;gt; &amp;lt;li &amp;gt;&amp;lt;i class= "Layui-icon" &amp;gt;&amp;amp; #xe63a;

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.