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

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> </div> </div> <!-- m3 END --> <div class="page"> <ul class="clearfix m01"> <li> <a href="/10752043/viewspace-992844/">天方夜谭VCL: 开门 (转)</a> </li> <li> <a href="/10752043/viewspace-992846/">天方夜谭VCL: 生死 (转)</a> </li> </ul> </div> </div> <footer> <a href="http://www.itpub.net">ITPUB论坛</a> | <a href="http://blog.chinaunix.net/">chinaunix博客</a> | <a href="http://bbs.chinaunix.net/">chinaunix论坛</a><br> 北京皓辰网域网络信息技术有限公司. 版权所有 </footer> <script> $(function(){ $("#fenlei").click(function(){ $("#left_1").show(); $("#left_2").show(); }); }) </script> </body> </html> <script language="javascript" src="http://stat.it168.com/pv.js"></script> <script> function sendPV(){ var pvTrack = new PvTrack(); pvTrack.type = 35; // 频道类别ID pvTrack.channel = 532; // 频道ID pvTrack.pageType = 0; pvTrack.track(); } window.setTimeout("sendPV()", 1000); </script> <div class="share_box"> <div class="share"> <div class="share_tit">分享到</div> <div class="share_close"></div> <ul class="share_list"> <a href="http://service.weibo.com/share/share.php?url=http://m.blog.itpub.net/10752043/viewspace-992845&appkey=&title=php的汉字转换: Unicode(UTF8)-&gt;GBK (转)&language=zh_cn"><li><span class="weibo">新浪微博</span></li></a> <a href="http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=http://m.blog.itpub.net/10752043/viewspace-992845&title=php的汉字转换: Unicode(UTF8)-&gt;GBK (转)&desc=php的汉字转换: Unicode(UTF8)-&gt;GBK (转)[@more@]秋水无恨 GBK Unicode UTF8 汉字 转换 PHP的汉字转换一直是比较麻烦的事 该类内置了四个过滤"&amp;#[dec];","&amp;#x[hex];","%u[hex]","utf8转换"方便用户的使用,同时也可自定义过滤进行自己喜欢的操作 qswhU.php 从这里下载idea.com/user/qswh/qswhU.zip"&gt;http://www.blueidea.com/user/qswh/qswhU.zip class qswhU{&nbsp;var $qswhData;&nbsp;function qswhU($filename="qswhU.php"){&nbsp;$this-&gt;qswhData=file($filename);&nbsp;}&nbsp;&nbsp;function decode($str,$p&summary=&site=&pics="><li><span class="qq"> QQ空间</span></li></a> </ul> <div class="weixin2"> <em></em> <i>请使用浏览器的分享功能分享到微信等</i></div> </div> </div> <script> $(".fenxiang").click(function(){ $(".share_box").show(); }); $(".share_close").click(function(){ $(".share_box").hide(); }); $(".backTop").click(function(){ $('html,body').animate({ scrollTop: 0 }, 300) }) $('.content p br').parents('p').each(function (k,v) { if ($(v).html() == '') { $(v).css('display','none') } }) </script> <script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script> <script> var videoContainer = {}; $(document).delegate('.fake-video-btn', 'click', function () { var width = $(this).parent('.video-container').width(); var height = $(this).parent('.video-container').height(); var $this = $(this); var videoid = $(this).data('videoid'); var aliplayerid = $this.closest('.video-container').attr('id'); $.ajax({ url: 'http://blog.itpub.net/getvidoeinfo?videoid=' + videoid , type: 'get' , success: function (ret) { if (ret.code == 200) { videoContainer[videoid] = new Aliplayer({ id:aliplayerid, autoplay:true, width:width + 'px', height:height + 'px', format: "", vid:videoid, playauth:ret.data.PlayAuth, cover:ret.data.VideoMeta.CoverURL, skinLayout:[ //按钮UI {name:"bigPlayButton","align":"cc","x":15,"y":12},//播放 {name: "H5Loading", align: "cc"}, {name: "errorDisplay", align: "tlabs", x: 0, y: 0}, {name: "infoDisplay"}, {name:"tooltip", align:"blabs",x: 0, y: 56}, {name: "thumbnail"}, { name: "controlBar", align: "blabs", x: 0, y: 0, children: [ {name: "progress", align: "blabs", x: 0, y: 44}, {name: "playButton", align: "tl", x: 15, y: 12}, {name: "timeDisplay", align: "tl", x: 10, y: 7}, {name: "fullScreenButton", align: "tr", x: 10, y: 12}, {name:"subtitle", align:"tr",x:15, y:12}, {name:"setting", align:"tr",x:15, y:12}, {name: "volume", align: "tr", x: 5, y: 10} ] } ] },function(player){ $this.siblings('img').remove(); $this.remove(); player.on('play',function(e) { for (var key in videoContainer) { if (videoid != key) { videoContainer[key].pause(); } } }); }); } else { alert(ret.msg); } } }) }); wx.config({ debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。 appId: 'wx4d058320abe98d01', // 必填,公众号的唯一标识 timestamp: 1755123979, // 必填,生成签名的时间戳 nonceStr: 'VNEGVJtAeBvf0sQZ', // 必填,生成签名的随机串 signature: '82b94abd055785ca67f8550cddde5991f2386f8b',// 必填,签名,见附录1 jsApiList: [ 'onMenuShareTimeline', 'onMenuShareAppMessage' ] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2 }); wx.ready(function(){ // config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。 var title = 'php的汉字转换: Unicode(UTF8)-&gt;GBK (转)'; var desc = ""; var link = 'http://m.blog.itpub.net/10752043/viewspace-992845/'; var imgUrl = $(".main img:first").attr('src'); if(!imgUrl){ imgUrl = 'http://m.blog.itpub.net/static/images/share.png?v1'; } console.log(imgUrl); wx.onMenuShareAppMessage({ //分享给朋友 title: title, // 分享标题,可在控制器端传递 desc: desc,//分享的描述,可在控制器端传递 link: link, // 分享链接,可在控制器端传递 imgUrl: imgUrl, // 分享图标 ,可在控制器端传递 success: function() {}, cancel: function() {} }); wx.onMenuShareTimeline({ //分享到朋友圈 title: title, // 分享标题,可在控制器端传递 desc: desc,//分享的描述,可在控制器端传递 link: link, // 分享链接,可在控制器端传递 imgUrl: imgUrl, // 分享图标 ,可在控制器端传递 success: function() {}, cancel: function() {} }); }); </script> </body> </html>