php 汉字转温度,php的汉字转换: GBK->Big5_php
发布日期:2021-06-24 13:06:05 浏览次数:3 分类:技术文章

本文共 1767 字,大约阅读时间需要 5 分钟。

php的汉字转换一直是比较麻烦的事

首先要说两点:

1.GBK的繁体字并不是Big5,但一般繁体字都有对应的Big码

2.一GBK码并不一定有对应的Big5码,所以一般是不可逆转换

该类内置了三个函数

qswhStr 格式化字符串

qswhFile 格式化一文件

qswhDir 格式化目录下所有文件(.qswh的除外,那是原先文档的备份)

qswhBig5.php 从这里下载

http://www.gaodaima.com/46747.htmlphp的汉字转换: GBK->Big5_php

http://www.blueidea.com/user/qswh/qswhBig5.zip

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

if(($p=ord(substr($gb,$i,1)))>127){

$q=ord(substr($gb,++$i,1));

$tmp=$this->qswhData[$p-128];

for($j=0;$j=$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("

请选择编码Big5查看:".$qswh->qswhStr($words));

echo("/n编码一目录:".$qswh->qswhDir("test"));

echo("/n编码一文件:".$qswh->qswhFile("index.htm"));

使用说明:

qswhStr中第二个参数代表如果没有对应的Big5将替换的字,如果为空,则保留该字

将文件格式化后,记得要修改

欢迎大家阅读《php的汉字转换: GBK->Big5_php》,跪求各位点评,若觉得好的话请收藏本文,by 搞代码

e7ce419cf2d6ad34d01da2ceb8829eed.png

微信 赏一包辣条吧~

023a57327877fb4402bcc76911ec18ea.png

支付宝 赏一听可乐吧~

转载地址:https://blog.csdn.net/weixin_32946543/article/details/115907583 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:php 查看memcache,怎样查看memcache的日志
下一篇:php 无限分类页面怎么输出,php无限分类且支持输出树状图的详细介绍_PHP

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2024年04月19日 22时21分43秒