<?
/**** 繁体<==>简体 ****/
/**
码表的取得:
http://netcity.hinet.net/kstchieh/table.zip
包中有两个文件:big5-gb.table,gb-big5.table
函数gb2big5由于没有big5环境,故未能详细测试
*/
// 将码表予装入内存
$filename = "big5-gb.table";
$fp = fopen($filename, "rb");
$big5 = fread($fp,filesize($filename));
fclose($fp);
$filename = "gb-big5.table";
$fp = fopen($filename, "rb");
$gb = fread($fp,filesize($filename));
fclose($fp);
/**
Big5码转换成GB码
*/
function big52gb($Text) {
global $big5;
$max = strlen($Text)-1;
for($i=0;$i<$max;$i++) {
$h = ord($Text[$i]);
if($h>=160) {
$l = ord($Text[$i+1]);
if($h==161 && $l==64) {
$gb = " ";
}else{
$p = ($h-160)*510+($l-1)*2;
$gb = $big5[$p].$big5[$p+1];
}
$Text[$i] = $gb[0];
$Text[$i+1] = $gb[1];
$i++;
}
}
return $Text;
}
/**
GB码转换成Big5码
*/
function gb2big5($Text) {
global $gb;
$max = strlen($Text)-1;
for($i=0;$i<$max;$i++) {
$h = ord($Text[$i]);
if($h>=160) {
$l = ord($Text[$i+1]);
if($h==161 && $l==64) {
$big = " ";
}else{
$p = ($h-160)*510+($l-1)*2;
$big = $gb[$p].$gb[$p+1];
}
$Text[$i] = $big[0];
$Text[$i+1] = $big[1];
$i++;
}
}
return $Text;
}
?>
<?
/**** 测试 ****/
$a = "啊阿埃挨哎唉哀皑癌蔼矮艾碍爱隘鞍氨";
echo "原简体 $a<br>";
$a = "摆甁玼玸絁砾魔窯︺锚稲筰綽";
echo "原繁体 $a<br>";
$a=big52gb($a);
echo "转简体 $a<br>";
$a=gb2big5($a);
echo "转繁体 $a<br>";
?>