编码
big5,gb2312=>0);
while($row = mysql_fetch_array($rs)) {
$code[pack(h4,$row][gbk])] = pack(h4,$row[big5]);
}
mysql_close($conn);
$fp = fopen($filename,w);
fwrite($fp,serialize($code));
fclose($fp);
}else {
$fp = fopen($filename,r);
$code = unserialize(fread($fp,filesize($filename)));
fclose($fp);
}
/**
* 将输入串转换成相应编码
*/
function getchar($s) {
global $code;
$v = ;
for($i=0;$i if(ord($s[$i]) > 0x7f) {
$p = sprintf(%02x%02x,$s[$i],$s[$i+1]);
$p = $s[$i].$s[$i+1];
$ch = $code[$p];
if($ch == )
$ch = $s[$i].$s[$i+1];
$v .= $ch;
$i++;
}else
$v .= $s[$i];
}
return $v;
}
/**
* 设定转换类型并输出头
* 此函数前不能有其他输出
*/
function setchar($char=gb2312) {
global $code;
if($char != gb2312 && $char != big5)
die(字符编码类型错!!!);
if($code[0] != $char)
$code = array_flip($code); // 若不是指定的编码类型,则交换键
header(content-type: text/html; charset=$char);
}
?>
测试例: