首页 > CMS > 织梦DEDE > 正文

织梦之GB转UTF-8编码

2024-08-30 19:06:34
字体:
来源:转载
供稿:网友
  • 本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。
  • /*******************************
    //gb转utf-8编码
    *******************************/
    function gb2utf8($gbstr) {
     global $codetable;
     if(trim($gbstr)=="") return $gbstr;
     if(empty($codetable)){
      $filename = dirname(__file__)."/data/gb2312-utf8.table";
      $fp = fopen($filename,"r");
      while ($l = fgets($fp,15))
      { $codetable[hexdec(substr($l, 0, 6))] = substr($l, 7, 6); }
      fclose($fp);
     }
     $ret = "";
     $utf8 = "";
     while ($gbstr) {
      if (ord(substr($gbstr, 0, 1)) > 0x80) {
       $thisw = substr($gbstr, 0, 2);
       $gbstr = substr($gbstr, 2, strlen($gbstr));
       $utf8 = "";
       @$utf8 = u2utf8(hexdec($codetable[hexdec(bin2hex($thisw)) - 0x8080]));
       if($utf8!=""){
        for ($i = 0;$i < strlen($utf8);$i += 3)
         $ret .= chr(substr($utf8, $i, 3));
       }
      }
      else
      {
       $ret .= substr($gbstr, 0, 1);
       $gbstr = substr($gbstr, 1, strlen($gbstr));
      }
     }
     return $ret;
    }
    发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表