ITPub博客

首页 > 应用开发 > IT综合 > php的汉字转换: Unicode(UTF8)->GBK (转)

php的汉字转换: Unicode(UTF8)->GBK (转)

原创 IT综合 作者:worldblog 时间:2007-12-13 13:03:43 0 删除 编辑
php的汉字转换: Unicode(UTF8)->GBK (转)[@more@]

秋水无恨 GBK Unicode UTF8 汉字 转换

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

该类内置了四个过滤"&#[dec];","&#x[hex];","%u[hex]","utf8转换"
方便用户的使用,同时也可自定义过滤进行自己喜欢的操作


qswhU.php 从这里下载
idea.com/user/qswh/qswhU.zip">http://www.blueidea.com/user/qswh/qswhU.zip

class qswhU{
 var $qswhData;
 function qswhU($filename="qswhU.php"){
 $this->qswhData=file($filename);
 }
 
 function decode($str,$pattern=0){
 $arr=array("/&#(w+);/iU","/((%ww)+)/i","/%u(w{4,5})/iU");
 if(is_integer($pattern)){
 if($pattern>=count($arr))die("Invalid Function");
 $pattern=$arr[$pattern];
 }
 return preg_replace_callback($pattern,array($this,"u2gb"),$str);
 }
 
 function u2gb($arr){
 /******(qiushuiwuhen 2002-8-15)******/
 $ret="";$str=$arr[1];
 if(preg_match_all("/%w{2}/",$str,$matches)){
 for($i=0;$i $chr1=hexdec(substr($matches[0][$i],1));
 $arr=array("f0","e0","c0","0");
 for($j=0;$jhexdec($arr[$j]))break;
 $chr=hexdec(substr($matches[0][$i],1))-hexdec($arr[$j]);
 while(++$j $str=dechex($chr);
 if(strlen($str)==4){
 $p=hexdec(substr($str,0,2))-0x4d;
 $q=hexdec(substr($str,2))*4;
 $ret.=chr(hexdec(substr($this->qswhData[$p],$q,2)));
 $ret.=chr(hexdec(substr($this->qswhData[$p],$q+2,2)));
 }else
 $ret.=chr(hexdec($str));
 }
 }
 else{ 
 if(strtolower($str[0])=="x")
 $str=substr($str,1);
 else
 if(strlen($str)!=4)$str=dechex($str);
 
 if(strlen($str)==4){
 $p=hexdec(substr($str,0,2))-0x4d;
 $q=hexdec(substr($str,2))*4;
 $ret.=chr(hexdec(substr($this->qswhData[$p],$q,2)));
 $ret.=chr(hexdec(substr($this->qswhData[$p],$q+2,2)));
 }else
 $ret.=chr(hexdec($str));
 }
 return $ret;
 }
 
 


使用范例

$qswh=new qswhU("qswhU.php");//如果文件名是qswhU.php,可省参数
echo "

不带参数(默认过滤为:&#[num];):";<BR>echo "n".$qswh->decode("&#x4E2D;&#x6587;&#x41;&#x62;&#x63;");<BR>echo "n".$qswh->decode("&#20013;&#25991;&#65;&#98;&#99;");<BR>echo "n<A href="tag-152-1.html">调用</A>内置过滤(UTF转码):".$qswh->decode("%E4%B8%AD%E6%96%87%20!%22%23%24%25%26'()*%2B%2C%2F%3A%3B%3C%3D%3E%3F%40%5B%5D%5E%60%7B%7C%7D~%25Abc",1);<BR>echo "n调用内置过滤unescape(%u[num]):".$qswh->decode("%u4E2D%u6587Abc",2);<BR>echo "n自定义过滤([x+num]):".$qswh->decode("[x4E2D][x6587][x41][x62][x63]","/[(w+)]/");</P> <P>效果如下:<BR>不带参数(默认过滤为:&#[num];):<BR>中文Abc<BR>中文Abc<BR>调用内置过滤(UTF转码):中文 !"#$%&'()*+,/:;<=>?@[]^`{|}~%Abc<BR>调用内置过滤unescape(%u[num]):中文Abc<BR>自定义过滤([x+num]):中文Abc</P> <P><BR>ps.该文章现仅限在csdn文档中心,如要转载,请和作者联系,否则后果自负。</P><BR></SPAN> <p style="clear:both;"></p> <p class="translate"> 来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752043/viewspace-992845/,如需转载,请注明出处,否则将追究法律责任。 </p> </div> </div> <div class="preview-footer clearfix"> <!--已点赞、收藏--> <div class="icon-praise" id="praise" onclick="blogObj.praise($(this),'/praise/')"><span class="text"></span><span class="num">0</span></div> <div class="icon-collect" id="collect" onclick="blogObj.praise($(this),'/favour/')"><span class="text"></span><span class="num">0</span></div> <div class="share-group fr"> <div class="bdsharebuttonbox bdshare-button-style0-16" data-bd-bind="1522216707615"> <span class="fl">分享到:</span> <a href="javascript:;" class="bds_tsina" data-cmd="tsina" title="分享到新浪微博"></a> <a href="javascript:;" class="bds_sqq" data-cmd="sqq" title="分享到QQ好友"></a> <a href="javascript:;" class="bds_qzone" data-cmd="qzone" title="分享到QQ空间"></a> <a href="javascript:;" class="bds_weixin" data-cmd="weixin" title="分享到微信"></a> </div> <script> window._bd_share_config = { "common": { "bdSnsKey": {}, "bdText": "", "bdDesc": "", "bdMini": "", "bdMiniList": false, "bdPic": "", "bdStyle": "0", "bdSize": "32" }, "share": {"bdSize": 16} }; with (document) 0[(getElementsByTagName('head')[0] || body).appendChild(createElement('script')).src = 'http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion=' + ~(-new Date() / 36e5)]; if ($(window).width() < 1320) { if ($(window).width() < 1200) { $(".sideslip").css({ "left": "20px", "margin-left": 0 }) } else { $(".sideslip").css({ "margin-left": "-590px" }) } }</script> </div> </div> <div class="article-context"> <div class="fl"> <span class="color77">上一篇:</span> <a href="http://blog.itpub.net/10752043/viewspace-992844/">天方夜谭VCL: 开门 (转)</a> </div> <div class="fr"> <span class="color77">下一篇:</span> <a href="http://blog.itpub.net/10752043/viewspace-992846/">天方夜谭VCL: 生死 (转)</a> </div> </div> <input type="hidden" id="blogId" value="992845"> <!--评论开始--> <div class="blog-comment"> <div class="new-comment"> <input type="hidden" id="hid" name="hid" value="992845"> <input type="hidden" name="_token" value="QhZgkTI5R9Kzbg6j62qBI9YMxerCYLkZQXUZU6eA"> <a class="avatar" href="javascript:;"> <img src="http://blog.itpub.net/images/user_pic_default.png"></a> <!--用户未登录--> <div class="sign-container"> <span>请登录后发表评论</span> <a class="btn btn-sign"> <a class="layui-btn ml30" onclick="blog.login(event)">登录</a> </a> </div> <!--用户已登录--> <textarea placeholder="请写下你的评论…" onfocus="blogObj.textareaFocus($(this),200)" maxlength=200></textarea> </div> <div class="comment-list"> <div class="top-title"><span class="all-comment">全部评论</span> <span class="colorbb ml20"></span> </div> <div class="comment-items" id="comment_items"> <script type="text/template" id='blog_template'> <%for(var i=0;i <items.length;i++){%> <div class="comment-item"> <div class="author"> <a target="_blank" href="/<%=items[i].uid%>" class="avatar"> <img src="<%=items[i].headimg%>" alt=""> </a> <div class="info"> <div class="name"><a target="_blank" href="/<%=items[i].uid%>"><%=items[i].username%></a> <span class="fr reply-btn" onclick="blogObj.newComment($(this),'<%=items[i].username%>',<%=items[i].id%>)">回复</span></div> <div class="time colorbb"><%=items[i].createtime%></div> </div> </div> <p class="comment-wrap"><%=items[i].content%></p> <%if(items[i].items.items.length) { %> <div class="sub-comment-list"> <%for(var j=0;j<items[i].items.items.length;j++){%> <div class="sub-comment"> <div class="color77 time-reply"> <span class="time"><%=items[i].items.items[j].createtime%></span> <span class="reply-btn fr" onclick="blogObj.newComment($(this),'<%=items[i].items.items[j].username%>',<%=items[i].items.items[j].id%>)">回复</span> </div> <p><a href="/<%=items[i].uid%>"><%=items[i].items.items[j].username%></a>&nbsp;&nbsp;&nbsp;回复&nbsp;&nbsp;&nbsp;<a href="/<%=items[i].touid%>"><%=items[i].items.items[j].tousername%></a>: <span class="content"><%=items[i].items.items[j].content%></span></p> </div> <%}%> <%if(items[i].items.total > 5) { %> <div class="more-comment"><span class="sub-comment-count">还有<i class="count"><%=items[i].items.total-5%></i>条评论</span><span class="more-comment-btn" onclick=blogObj.loadSubComment($(this),<%=items[i].id%>) data-count=1 data-flag=true>点击查看</span></div> <%}%> </div> <%}%> </div> <%}%> </script> </div> </div> </div> <!--评论结束--> </div> <script> </script> <div class="fr w290"> <!--作者信息开始--> <!--作者信息开始--> <div class="author-info right-fixed "> <div class="head-img"> <a href="http://blog.itpub.net/10752043/"><img src="http://account.itpub.net/api/avatar.php?uid=10752043" alt=""></a> </div> <div class="author-name"><a href="http://blog.itpub.net/10752043/">worldblog</a></div> <div class="author-intro"> </div> <ul class="tree-list clearfix"> <li> <div class="item-tt">博文量</div> <a href="http://blog.itpub.net/10752043/"><span class="item-num blognum">6241</span></a> </li> <li> <div class="item-tt">访问量</div> <div class="item-num blogviewnum">2405703</div> </li> </ul> </div> <!--作者信息结束--> <script> var data = {uid: 10752043}; $._ajax({ url: '/getAuthorInfo/', data: data, type: 'get', dataType: 'json', success: function (data) { if (data.code == 200) { var data = data.data; $(".blognum").text(data.blognum); $(".blogviewnum").text(data.visitednum); } } }); </script> <!--作者信息结束--> <!--博文推荐开始--> <div class="blog-choice right-fixed"> <h3 class="choice-title">最新文章</h3> <ul class="newul"> <li class="new-item"> <a href="http://blog.itpub.net/10752043/viewspace-1005762/" class="clearfix"> 可怕的 C# (转) </a> </li> <li class="new-item"> <a href="http://blog.itpub.net/10752043/viewspace-1005755/" class="clearfix"> Unify the Role-Based Security Models for Enterprise and Application Domains with .NET (转) </a> </li> <li class="new-item"> <a href="http://blog.itpub.net/10752043/viewspace-1005754/" class="clearfix"> Building Secure ASP.NET Applications: Authentication, Authorization, and Secure Communication (转) </a> </li> <li class="new-item"> <a href="http://blog.itpub.net/10752043/viewspace-1005752/" class="clearfix"> [Sample] Playing with music file (转) </a> </li> <li class="new-item"> <a href="http://blog.itpub.net/10752043/viewspace-1005751/" class="clearfix"> 中文web-app_2_3.dtd (转) </a> </li> <li class="new-item"> <a href="http://blog.itpub.net/10752043/viewspace-1005750/" class="clearfix"> 使用ASP.NET加密口令 (转) </a> </li> <li class="new-item"> <a href="http://blog.itpub.net/10752043/viewspace-1005748/" class="clearfix"> C++ Builder 高手进阶 (五)用BCB编写多线程应用程序 (转) </a> </li> <li class="new-item"> <a href="http://blog.itpub.net/10752043/viewspace-1005747/" class="clearfix"> 24点游戏探秘系列6:用概率统计的眼光看24点游戏 (转) </a> </li> <li class="new-item"> <a href="http://blog.itpub.net/10752043/viewspace-1004904/" class="clearfix"> Schema-oriented message destination (转) </a> </li> <li class="new-item"> <a href="http://blog.itpub.net/10752043/viewspace-1004903/" class="clearfix"> J2EE vs. Microsoft.NET (转) </a> </li> <ul> </div> <!--大牛精选结束--> </div> </div> <div class="icon-back-top" onclick="$('html,body').animate({scrollTop:0},'slow');"></div> <!--main部分结束--> <!--百度推送--> <script> (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> <!--2019-09-21 17:52:31--> <!--footer部分开始--> <div class="blog-footer"> <div class="w1200 pr"> <div class="footer-links"> <a class="icon-blog icon-wx mr30"> <div class="wx-qrcode"> <img src="http://edu.itpub.net/images/qrcode.jpg" alt=""> </div> </a> <a href="https://weibo.com/itpub2001?from=myfollow_all" class="icon-blog icon-wb" target="_blank"></a> </div> <p class="footer-nav"> <a href="http://www.it168.com/bottomfile/it168.shtml" target="_blank">支持我们</a> <a href="http://www.it168.com/bottomfile/tgzn.shtml" target="_blank">作者招募</a> <a href="http://www.it168.com/bottomfile/sytk.shtml" target="_blank">用户协议</a> <a href="http://blog.itpub.net/31509949/viewspace-2157750/" target="_blank">FAQ</a> <a href="http://edu.itpub.net/contactus.html" target="_blank">Contact Us</a> <script src="https://s22.cnzz.com/z_stat.php?id=1274521965&web_id=1274521965" language="JavaScript"></script> </p> <p>北京盛拓优讯信息技术有限公司. 版权所有&nbsp;&nbsp;<a style="color:#777777;" target="_blank" href="http://beian.miit.gov.cn">京ICP备09055130号-4</a>&nbsp;&nbsp;北京市公安局海淀分局网监中心备案编号:11010802021510</p> <p>广播电视节目制作经营许可证(京) 字第1234号 中国互联网协会会员</p> </div> </div> <!--footer部分结束--> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?5016281862f595e78ffa42f085ea0f49"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> <!-- END STAT PV --> </body> </html>