[PHP] gb2312编码转utf8编码 →→→→→进入此内容的聊天室

来自 , 2020-07-06, 写在 PHP, 查看 108 次.
URL http://www.code666.cn/view/0fc170ec
  1. //gb2312编码转utf8编码
  2. function gb2utf8($gb)/* Program writen by sadly   modified by agun */{
  3.     if (!trim($gb))
  4.         return $gb;
  5.     $filename = "gb2312.txt";
  6.     $tmp = file($filename);
  7.     $codetable = array();
  8.     while (list($key, $value) = each($tmp))
  9.         $codetable[hexdec(substr($value, 0, 6))] = substr($value, 7, 6);
  10.    
  11.     $ret = "";
  12.     $utf8 = "";
  13.     while ($gb) {
  14.         if (ord(substr($gb, 0, 1)) > 127) {
  15.             $this = substr($gb, 0, 2);
  16.             $gb = substr($gb, 2, strlen($gb));
  17.             $utf8 = u2utf8(hexdec($codetable[hexdec(bin2hex($this)) - 0x8080]));
  18.             for ($i = 0; $i < strlen($utf8); $i += 3)
  19.                 $ret. = chr(substr($utf8, $i, 3));
  20.         } else {
  21.             $ret. = substr($gb, 0, 1);
  22.             $gb = substr($gb, 1, strlen($gb));
  23.         }
  24.     }
  25.     return $ret;
  26. }
  27.  
  28.  
  29.  
  30. //unicode编码转utf8编码
  31. function u2utf8($c) {
  32.     for ($i = 0; $i < count($c); $i++)
  33.         $str = "";
  34.     if ($c < 0x80) {
  35.         $str. = $c;
  36.     } else if ($c < 0x800) {
  37.         $str. = (0xC0 | $c >> 6);
  38.         $str. = (0x80 | $c & 0x3F);
  39.     } else if ($c < 0x10000) {
  40.         $str. = (0xE0 | $c >> 12);
  41.         $str. = (0x80 | $c >> 6 & 0x3F);
  42.         $str. = (0x80 | $c & 0x3F);
  43.     } else if ($c < 0x200000) {
  44.         $str. = (0xF0 | $c >> 18);
  45.         $str. = (0x80 | $c >> 12 & 0x3F);
  46.         $str. = (0x80 | $c >> 6 & 0x3F);
  47.         $str. = (0x80 | $c & 0x3F);
  48.     }
  49.     return $str;
  50. }
  51.  

回复 "gb2312编码转utf8编码"

这儿你可以回复上面这条便签

captcha