首頁  >  文章  >  後端開發  >  php汉字转码 GBK->Unicode(UTF8)编码转换

php汉字转码 GBK->Unicode(UTF8)编码转换

WBOY
WBOY原創
2016-07-25 08:53:531647瀏覽
  1. //php汉字转码 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 if(($p=ord(substr($gb,$i,1)))>127){
  11. $q=ord(substr($gb,++$i,1));
  12. $q=($q-($q>128?65:64))*4;
  13. $q=substr($this->qswhData[$p-128],$q,4);
  14. }
  15. else
  16. $q=dechex($p);
  17. if(empty($callback))
  18. $ret.=$q;
  19. else {
  20. $arr=array("htmlHex","htmlDec","escape","u2utf8");
  21. if(is_integer($callback)){
  22. if($callback>count($arr))die("Invalid Function");
  23. $ret.=$this->$arr[$callback-1]($q);
  24. }else
  25. $ret.=$callback($q);
  26. }
  27. }
  28. return $ret;
  29. }
  30. function htmlHex($str){
  31. return "".$str.";";
  32. } // 程序员之家 bbs.it-home.org
  33. function htmlDec($str){
  34. return "".hexdec($str).";";
  35. }
  36. function escape($str){
  37. return hexdec($str) }
  38. function u2utf8($str){
  39. /******(qiushuiwuhen 2002-8-15)******/
  40. $sp="!'()*-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~";
  41. $dec=hexdec($str);
  42. $bin=decbin($dec);
  43. $len=strlen($bin);
  44. $arr=array("c0","e0","f0");
  45. if($dec>0x7f){
  46. $ret="";
  47. for($i=$len,$j=-1;$i>=0;$i-=6,$j++){
  48. if($i>6)
  49. $ret="%".dechex(0x80+bindec(substr($bin,$i-6,6))).$ret;
  50. else
  51. $ret="%".dechex(hexdec($arr[$j])+bindec(substr($bin,0,6-$i))).$ret;
  52. }
  53. }else{
  54. if(strpos($sp,chr($dec)))
  55. $ret=chr($dec);
  56. else
  57. $ret="%".strtolower($str);
  58. }
  59. return $ret;
  60. }
  61. }
复制代码

调用例子:

  1. $words="中文Abc";

  2. function ex($str){return "[".$str."]";}
  3. $qswh=new qswhGBK("qswhGBK.php");//如果文件名是qswhGBK.php,可省参数

  4. echo("

    不带参数:".$qswh->gb2u($words));
  5. echo("/n调用内置函数htmlHex:".$qswh->gb2u($words,1));
  6. echo("/n调用内置函数htmlDec:".$qswh->gb2u($words,2));
  7. echo("/n调用内置函数escape:".$qswh->gb2u($words,3));
  8. echo("/n调用内置函数u2utf8:".$qswh->gb2u($words,4));
  9. echo("/n调用自定义函数:".$qswh->gb2u($words,ex));
复制代码

效果:

不带参数:4E2D6587416263 调用内置函数htmlHex:中文Abc 调用内置函数htmlDec:中文Abc 调用内置函数escape:%u4E2D%u6587Abc 调用内置函数u2utf8:%e4%b8%ad%e6%96%87Abc 调用自定义函数:[4E2D][6587][41][62][63]


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn