编程

当前位置:澳门新莆京娱乐官网 > 编程 > 不用iconv函数实现UTF

不用iconv函数实现UTF

来源:http://www.drgigabytes.com 作者:澳门新莆京娱乐官网 时间:2019-12-29 03:08

倘诺使用 iconv()函数转变编码就相比易了,不过超多设想主机里并不援救那么些组件,笔者在网络找半天,才找到二个gb2312转utf-8的方法,但不能逆向转变。那个函数如下:/*******************************//GB转UTF-8编码*******************************/function gb2utf8($gbstr) {global $CODETABLE;if(trim($gbstr)==) return $gbstr;if(empty($CODETABLE)){$filename = dirname(__FILE__)./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)) 127) {$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;}//Unicode转utf8function u2utf8($c) {for ($i = 0;$i count($c);$i )$str = ;if ($c 0x80) {$str .= $c;} else if ($c 0x800) {$str .= (0xC0 | $c 6);$str .= (0x80 | $c 0x3F);} else if ($c 0x10000) {$str .= (0xE0 | $c 12);$str .= (0x80 | $c 6 0x3F);$str .= (0x80 | $c 0x3F);} else if ($c 0x200000) {$str .= (0xF0 | $c 18);$str .= (0x80 | $c 12 0x3F);$str .= (0x80 | $c 6 0x3F);$str .= (0x80 | $c 0x3F);}

本文由澳门新莆京娱乐官网发布于编程,转载请注明出处:不用iconv函数实现UTF

关键词: