日期:2014-05-17  浏览次数:20966 次

求一个ASP编码转换函数,能把BIG5码转换成UTF-8格式的
谢谢

------解决方案--------------------

/**********************************
//Big5码转换成GB码
**********************************/
function big52gb($Text) {
global $BIG5_DATA;
if(empty($BIG5_DATA)){
$filename = dirname(__FILE__). "/table/big5-gb.table ";
$fp = fopen($filename, "rb ");
$BIG5_DATA = fread($fp,filesize($filename));
fclose($fp);
}
$max = strlen($Text)-1;
for($i=0;$i <$max;$i++) {
$h = ord($Text[$i]);
if($h> =0x80) {
$l = ord($Text[$i+1]);
if($h==161 && $l==64) {
$gbstr = "  ";
}else{
$p = ($h-160)*510+($l-1)*2;
$gbstr = $BIG5_DATA[$p].$BIG5_DATA[$p+1];
}
$Text[$i] = $gbstr[0];
$Text[$i+1] = $gbstr[1];
$i++;
}
}
return $Text;
}


/*******************************
//GB转UTF-8编码
*******************************/
function gb2utf8($gbstr) {
global $CODETABLE;
if(trim($gbstr)== " ") return $gbstr;
if(empty($CODETABLE)){
$filename = dirname(__FILE__). "/table/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;
}
//Unicode转utf8
function 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);
}
return $str;
}