当前位置:编程学习 > php >>

php的汉字转换GBK-UnicodeUTF8


php的汉字转换一直是比较麻烦的事
该类内置了四个函数"htmlHex","htmlDec","escape","u2utf8"
方便用户的使用,同时也可自定义函数进行自己喜欢的操作
qswhGBK.php 从这里下载
http://www.blueidea.com/user/qswh/qswhGBK.zip
class qswhGBK{
var $qswhData;
function qswhGBK($filename="qswhGBK.php"){
$this->qswhData=file($filename);
}
function gb2u($gb,$callback=""){
/******(qiushuiwuhen 2002-8-15)******/
$ret="";
for($i=0;$i if(($p=ord(substr($gb,$i,1)))>127){
$q=ord(substr($gb,++$i,1));
$q=($q-($q>128?65:64))*4;
$q=substr($this->qswhData[$p-128],$q,4);
}
else
$q=dechex($p);
if(empty($callback))
$ret.=$q;
else {
$arr=array("htmlHex","htmlDec","escape","u2utf8");
if(is_integer($callback)){
if($callback>count($arr))die("Invalid Function");
$ret.=$this->$arr[$callback-1]($q);
}else
$ret.=$callback($q);
}
}
return $ret;


}
function htmlHex($str){
return "&#x".$str.";";
}
function htmlDec($str){
return "&#".hexdec($str).";";
}
function escape($str){
return hexdec($str)<256?chr(hexdec($str)):"%u".$str;
}
function u2utf8($str){
/******(qiushuiwuhen 2002-8-15)******/
$sp="!()*-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~";
$dec=hexdec($str);
$bin=decbin($dec);
$len=strlen($bin);
$arr=array("c0","e0","f0");
if($dec>0x7f){
$ret="";
for($i=$len,$j=-1;$i>=0;$i-=6,$j++){
if($i>6)
$ret="%".dechex(0x80+bindec(substr($bin,$i-6,6))).$ret;
else
$ret="%".dechex(hexdec($arr[$j])+bindec(substr($bin,0,6-$i))).$ret;
}
}else{
if(strpos($sp,chr($dec)))
$ret=chr($dec);
else
$ret="%".strtolower($str);
}
return $ret;
}
}
使用范例
$words="中文Abc";
function ex($str){return "[".$str."]";}
$qswh=new qswhGBK("qswhGBK.php");//如果文件名是qswhGBK.php,可省参数echo("不带参数:".$qswh->gb2u($words));echo(" 调用内置函数htmlHex:".$qswh->gb2u($words,1));echo(" 调用内置函数htmlDec:".$qswh->gb2u($words,2));echo(" 调用内置函数escape:".$qswh->gb2u($words,3));echo(" 调用内置函数u2utf8:".$qswh->gb2u($words,4));echo(" 调用自定义函数:".$qswh->gb2u($words,ex));效果如下:不带参数:4E2D6587416263调用内置函数htmlHex:&#x4E2D;&#x6587;&#x41;&#x62;&#x63;调用内置函数htmlDec:&#20013;&#25991;&#65;&#98;&#99;调用内置函数escape:%u4E2D%u6587Abc调用内置函数u2utf8:%e4%b8%ad%e6%96%87Abc调用自定义函数:[4E2D][6587][41][62][63]ps.该文章现仅限在csdn文档中心,如要转载,请和作者联系,否则后果自负。<pre><span>补充:</span>Web开发 , php ,</pre></div><div class="zykmore"><script type="text/javascript" src="/js/s3.js"></script></div><div class="content"><p>上一个:<a href="/show/e610d42de7445bfd.htm">使用PHP4中的IntegratedTemplate类实现HTML和PHP代码分离</a><br/>下一个:<a href="/show/b5130ffbb1a37d70.htm">php的汉字转换GBK-Big5</a></p><div class="piclist">更多图片编程知识:<ul><li><a href="/show/8bb8bf6298d8c6ed.htm"><img src='/upload/20130328/re5nohisko2.jpg' width=105 height=105><br/>PHP文件是什么,怎样打开PHP文件</a></li><li><a href="/show/070123873bc17c27.htm"><img src='/upload/2013111604/20120803090937524.jpg' width=105 height=105><br/>使用PHP解析行政区划代码</a></li><li><a href="/show/a6732b531ac3ee9f.htm"><img src='/upload/2013111604/20111122040934622.jpg' width=105 height=105><br/>Eclipse开发PHP环境配置</a></li><li><a href="/show/63fe9b4fb03f4c92.htm"><img src='/upload/2013111613/20110813020002633.jpg' width=105 height=105><br/>php调用My97DatePicker</a></li><li><a href="/show/22c2574f33331d28.htm"><img src='/upload/2013111613/2008042200322771.gif' width=105 height=105><br/>让PHP网站跑的更快---如何优化PHP</a></li><li><a href="/show/93143c076cfd8e29.htm"><img src='/upload/2013111614/spacer.gif' width=105 height=105><br/>Classes and Objects in PHP5</a></li></ul></div></div> <div class="zykmore"><script type="text/javascript" src="/js/s4.js"></script></div><div class="more"><dl><dt>更多<a href="/list/2_1.htm">php</a>疑问解答:</dt><dd><a target="_blank" href="/show/43ebe0f31fc162f7.htm">php使用imagick将image图片转化为字符串模式</a></dd><dd><a target="_blank" href="/show/228688b59ba70796.htm">php通过gd实现图片图片转换为字符图代码</a></dd><dd><a target="_blank" href="/show/43ebe0f31fc162f7.htm">PHP把图片转base64代码,php把base64代码转换为图片并保存</a></dd><dd><a target="_blank" href="/show/b636313fd0bf206e.htm">PHP把图片base64转换成图片并保存成文件</a></dd><dd><a target="_blank" href="/show/a0b923820dcc509a.htm">wordpress问题&lt;?php if(have_posts()) : ?&gt;</a></dd><dd><a target="_blank" href="/show/9d4c2f636f067f89.htm">建设一个搜索类网站php还是jsp,数据库那个好</a></dd><dd><a target="_blank" href="/show/a2f3e71d9181a67b.htm">没理由啊 php代码无法执行,貌似有语法错误。。。</a></dd><dd><a target="_blank" href="/show/bbce2345d7772b06.htm">关于PHP 和API 的一段代码不懂啊不懂,请高手指点! 这是淘宝API的</a></dd><dd><a target="_blank" href="/show/5a880faf6fb5e608.htm">php语言中,序列化到底在那里使用?它的优势是什么?劣势是什么?</a></dd><dd><a target="_blank" href="/show/ceea167a5a36dedd.htm">PHP函数等于或等于应该怎么表达</a></dd><dd><a target="_blank" href="/show/fb98ab9159f51fd0.htm">请教php高手,解决basename函数和mb_substr函数处理中文文件名称的解决方法,在上传文件时,总是出现乱码</a></dd><dd><a target="_blank" href="/show/2e2d7fbdea1afc51.htm">.NET,PHP,JAVA,JS优秀点分别是?</a></dd><dd><a target="_blank" href="/show/02a44259755d38e6.htm">织梦cms 在环境监测的时候 wamp5 gd不支持 是为什么。;extension=php_gd2.dll这一句我删除了还是不显示?</a></dd><dd><a target="_blank" href="/show/d9caa6e02c990b0a.htm">我想学PHP。1.应安装什么编程工具? 2. 装LIUNX系统是装简易的还是?什么版本的?3.还应安装什么?</a></dd><dd><a target="_blank" href="/show/6fe97759aa27a0c9.htm">&lt;?php 和 &lt;? 有什么区别</a></dd></dl></div> </div> <div id="zykleft"> <div class="leftm"> <div id="logo"><a href="http://www.zzzyk.com/">站长资源库</a> <div class="clear"></div><script type="text/javascript" src="/js/i7f.js"></script></div> <script type="text/javascript" src="/js/sharen.js"></script><div class="clear"></div> <dl class="list"><dd><a href="/list/1_1.htm">asp</a></dd><dd><a href="/list/2_1.htm">php</a></dd><dd><a href="/list/3_1.htm">Delphi</a></dd><dd><a href="/list/4_1.htm">Matlab</a></dd><dd><a href="/list/5_1.htm">JSP</a></dd><dd><a href="/list/6_1.htm">Foxpro</a></dd><dd><a href="/list/7_1.htm">JS</a></dd><dd><a href="/list/8_1.htm">C/C++</a></dd><dd><a href="/list/9_1.htm">C#/ASP.NET</a></dd><dd><a href="/list/10_1.htm">VC++</a></dd><dd><a href="/list/11_1.htm">JAVA</a></dd><dd><a href="/list/12_1.htm">VB</a></dd><dd><a href="/list/13_1.htm">汇编语言</a></dd><dd><a href="/list/20_1.htm">html/css</a></dd><dd><a href="/list/21_1.htm">CGI</a></dd><dd><a href="/list/23_1.htm">XML/UML</a></dd><dd><a href="/list/24_1.htm">wap</a></dd><dd><a href="/list/27_1.htm">网站相关</a></dd><dd><a href="/list/60_1.htm">网页素材</a></dd><dd><a href="/list/70_1.htm">python</a></dd><dd><a href="/list/71_1.htm">微信小程序</a></dd><dd><a href="/list/72_1.htm">thinkphp</a></dd></dl><dl class="notice">如果你遇到<a href="/default.htm">编程学习</a>难题:<br/>访问www.zzzyk.com 试试</dl> </div> <div class="leftm"><dl><dt><script type="text/javascript" src="/js/s5.js"></script></dt></dl></div> <div class="leftm"><dl><dt><script type="text/javascript" src="/js/s6.js"></script></dt></dl></div> </div></div> <div class="clear"></div> <div class="bdshare"> <script type="text/javascript" src="/js/share.js"></script> </div> <div class="clear"></div> <div class="foot">CopyRight &copy; 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved<br/>部分文章来自网络,<script type="text/javascript" src="/images/my.js"></script> </div> </body> </html>