<?
//php汉字转码
class qswhBig5{
var $qswhData;
function qswhBig5($filename="qswhBig5.php"){
$this->qswhData=file($filename);
}
function qswhStr($gb,$fail="??"){
/******(qiushuiwuhen 2002-9-6)******/
$ret="";
for($i=0;$i<strlen($gb);$i++){
if(($p=ord(substr($gb,$i,1)))>127){
$q=ord(substr($gb,++$i,1));
$tmp=$this->qswhData[$p-128];
for($j=0;$j<strlen($tmp);$j+=6)if(($k=hexdec(substr($tmp,$j,2)))>=$q)
break;
if($k==$q)$q=chr(hexdec(substr($tmp,$j+2,2))).chr(hexdec(substr($tmp,$j+4,2)));
else if($fail=="")$q=chr($p).chr($q); else $q=$fail;
}
else
$q=chr($p);
$ret.=$q;
}
return $ret;
}
function qswhFile($filename,$fail="??"){
/******(qiushuiwuhen 2002-9-6)******/
if(!
file_exists($filename.".qswh"))copy($filename,$filename.".qswh");
$fp=fopen($filename,"r+");
$tmp=fread($fp,filesize($filename));
rewind($fp);
fwrite($fp,$this->qswhStr($tmp,$fail));
fclose($fp);
}
function qswhDir($dirname,$fail="??"){
/******(qiushuiwuhen 2002-9-6)******/
$d = dir($dirname);
while($entry=$d->read()) {
if($entry=="."||$entry=="..")
continue;
$entry=$dirname."/".$entry;
if(is_dir($entry))
$this->qswhDir($entry,$fail);
else{
if(substr($entry,-5)!=".qswh")$this->qswhFile($entry,$fail);
}
}
$d->close();
return $c;
}
}
$words="秋水无恨";
$qswh=new qswhBig5("qswhBig5.php"); //qswhBig5.php,可省参数
echo("<xmp>请选择编码Big5查看:".$qswh->qswhStr($words));
echo("/n编码一目录:".$qswh->qswhDir("test"));
echo("/n编码一文件:".$qswh->qswhFile("index.htm"));