php的汉字转换:GBK->Big5
时间:2007-02-17 来源:PHP爱好者
php的汉字转换一直是比较麻烦的事
chinaitpower.comye1LzXuPz5jg8xP1906YUyLiG
首先要说两点:
1.GBK的繁体字并不是Big5,但一般繁体字都有对应的Big码
2.一GBK码并不一定有对应的Big5码,所以一般是不可逆转换China It Power . Comye1LzXuPz5jg8xP1906YUyLiG 该类内置了三个函数
qswhStr 格式化字符串
qswhFile 格式化一文件
qswhDir 格式化目录下所有文件(.qswh的除外,那是原先文档的备份)China It Power . Comye1LzXuPz5jg8xP1906YUyLiG qswhBig5.php 从这里下载
http://www.blueidea.com/user/qswh/qswhBig5.zipChina It Power . Comye1LzXuPz5jg8xP1906YUyLiG <?
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;
}
}
China It Power . Comye1LzXuPz5jg8xP1906YUyLiG 使用范例:
$words="秋水无恨";
$qswh=new qswhBig5("qswhBig5.php");//qswhBig5.php,可省参数China It Power . Comye1LzXuPz5jg8xP1906YUyLiG echo("<xmp>请选择编码Big5查看:".$qswh->qswhStr($words));
echo("n编码一目录:".$qswh->qswhDir("test"));
echo("n编码一文件:".$qswh->qswhFile("index.htm"));
China It Power . Comye1LzXuPz5jg8xP1906YUyLiG 使用说明:
qswhStr中第二个参数代表如果没有对应的Big5将替换的字,如果为空,则保留该字
将文件格式化后,记得要修改<meta中的content-type为Big。China It Power . Comye1LzXuPz5jg8xP1906YUyLiG ps.该文章现仅限在csdn文档中心,如要转载,请和作者联系,否则后果自负。China It Power . Comye1LzXuPz5jg8xP1906YUyLiG
php爱好者站 http://www.phpfans.net Linux|Apache|IIS.
chinaitpower.comye1LzXuPz5jg8xP1906YUyLiG
首先要说两点:
1.GBK的繁体字并不是Big5,但一般繁体字都有对应的Big码
2.一GBK码并不一定有对应的Big5码,所以一般是不可逆转换China It Power . Comye1LzXuPz5jg8xP1906YUyLiG 该类内置了三个函数
qswhStr 格式化字符串
qswhFile 格式化一文件
qswhDir 格式化目录下所有文件(.qswh的除外,那是原先文档的备份)China It Power . Comye1LzXuPz5jg8xP1906YUyLiG qswhBig5.php 从这里下载
http://www.blueidea.com/user/qswh/qswhBig5.zipChina It Power . Comye1LzXuPz5jg8xP1906YUyLiG <?
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;
}
}
China It Power . Comye1LzXuPz5jg8xP1906YUyLiG 使用范例:
$words="秋水无恨";
$qswh=new qswhBig5("qswhBig5.php");//qswhBig5.php,可省参数China It Power . Comye1LzXuPz5jg8xP1906YUyLiG echo("<xmp>请选择编码Big5查看:".$qswh->qswhStr($words));
echo("n编码一目录:".$qswh->qswhDir("test"));
echo("n编码一文件:".$qswh->qswhFile("index.htm"));
China It Power . Comye1LzXuPz5jg8xP1906YUyLiG 使用说明:
qswhStr中第二个参数代表如果没有对应的Big5将替换的字,如果为空,则保留该字
将文件格式化后,记得要修改<meta中的content-type为Big。China It Power . Comye1LzXuPz5jg8xP1906YUyLiG ps.该文章现仅限在csdn文档中心,如要转载,请和作者联系,否则后果自负。China It Power . Comye1LzXuPz5jg8xP1906YUyLiG
php爱好者站 http://www.phpfans.net Linux|Apache|IIS.
相关阅读 更多 +