首页 > 编程 > PHP > 正文

php汉字转码 Unicode(UTF8)至GBK编码转换

2020-03-22 18:54:56
字体:
来源:转载
供稿:网友
  1. //php汉字转码
  2. html' target='_blank'>class qswhU{
  3. var $qswhData;
  4. function qswhU($filename="qswhU.php"){
  5. $this->qswhData=file($filename);
  6. }
  7. function decode($str,$pattern=0){
  8. $arr=array("/&#(/w+);/iU","/((%/w/w)+)/i","/%u(/w{4,5})/iU");
  9. if(is_integer($pattern)){
  10. if($pattern>=count($arr))die("Invalid Function");
  11. $pattern=$arr[$pattern];
  12. }
  13. return preg_replace_callback($pattern,array($this,"u2gb"),$str);
  14. } // 程序员之家 bbs.it-home.org
  15. function u2gb($arr){
  16. /******(qiushuiwuhen 2002-8-15)******/
  17. $ret="";$str=$arr[1];
  18. if(preg_match_all("/%/w{2}/",$str,$matches)){
  19. for($i=0;$i $chr1=hexdec(substr($matches[0][$i],1));
  20. $arr=array("f0","e0","c0","0");
  21. for($j=0;$jhexdec($arr[$j]))break;
  22. $chr=hexdec(substr($matches[0][$i],1))-hexdec($arr[$j]);
  23. while(++$j $str=dechex($chr);
  24. if(strlen($str)==4){
  25. $p=hexdec(substr($str,0,2))-0x4d;
  26. $q=hexdec(substr($str,2))*4;
  27. $ret.=chr(hexdec(substr($this->qswhData[$p],$q,2)));
  28. $ret.=chr(hexdec(substr($this->qswhData[$p],$q+2,2)));
  29. }else
  30. $ret.=chr(hexdec($str));
  31. }
  32. }
  33. else{
  34. if(strtolower($str[0])=="x")
  35. $str=substr($str,1);
  36. else
  37. if(strlen($str)!=4)$str=dechex($str);
  38. if(strlen($str)==4){
  39. $p=hexdec(substr($str,0,2))-0x4d;
  40. $q=hexdec(substr($str,2))*4;
  41. $ret.=chr(hexdec(substr($this->qswhData[$p],$q,2)));
  42. $ret.=chr(hexdec(substr($this->qswhData[$p],$q+2,2)));
  43. }else
  44. $ret.=chr(hexdec($str));
  45. }
  46. return $ret;
  47. }
  48. }
复制代码

使用实例:

  1. $qswh=new qswhU("qswhU.php");//如果文件名是qswhU.php,可省参数
  2. echo "不带参数(默认过滤为:&#[num];):";<li>echo "/n".$qswh->decode("&#x4E2D;&#x6587;&#x41;&#x62;&#x63;");<li>echo "/n".$qswh->decode("&#20013;&#25991;&#65;&#98;&#99;");<li>echo "/n调用内置过滤(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);<li>echo "/n调用内置过滤unescape(%u[num]):".$qswh->decode("%u4E2D%u6587Abc",2);<li>echo "/n自定义过滤([x+num]):".$qswh->decode("[x4E2D][x6587][x41][x62][x63]","//[(/w+)/]/");</ol><em onclick="copycode($('code_HJP'));">复制代码</em><p>效果如下:不带参数(默认过滤为:&#[num];):中文Abc中文Abc调用内置过滤(UTF转码):中文 !"#$%&'()*+,/:;<=>?@[]^`{|}~%Abc调用内置过滤unescape(%u[num]):中文Abc自定义过滤([x+num]):中文Abc</p></td></table> <p class="post-copyright">郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。</p> </SPAN> </div> </div> <div class=page1> <p align="center" class="pageLink"></p> </div> <div class="fenxiang"> <!--分享 --> <div style="text-align:left">上一篇:<a href='/wen/2020/03-23/752108.html'>PHP中文分词工具ICTCLAS的使用介绍_PHP教程</a></div> <br> <div style="text-align:left">下一篇:<a href='/wen/2020/03-23/752110.html'>PHP生成UTF-8编码的CSV文件打开乱码的解决方法</a></div> <br> <script src=/d/js/acmsd/gao21.js></script> <!--分享 --> </div> <div class="pinglun"> <!-- 评论 --> <script> function CheckPl(obj) { if(obj.saytext.value=="") { alert("您没什么话要说吗?"); obj.saytext.focus(); return false; } return true; } </script><form action="/e/pl/doaction.php" method="post" name="saypl" id="saypl" onsubmit="return CheckPl(document.saypl)"> <table width="100%" border="0" cellpadding="0" cellspacing="0" id="plpost"> <tr> <td><table width="100%" border="0" cellpadding="0" cellspacing="0" class="title"> <tr> <td><strong>发表评论</strong></td> <td align="right"><a href="/e/pl/?classid=45&amp;id=752109">共有<span><script type="text/javascript" src="/e/public/ViewClick/?classid=45&id=752109&down=2"></script></span>条评论</a></td> </tr> </table> <table width="100%" border="0" cellspacing="10" cellpadding="0"> <tr> <td><table width="100%" border="0" cellpadding="0" cellspacing="2"> <tr> <td width="56%" align="left">用户名: <input name="username" type="text" class="inputText" id="username" value="" size="16" /></td> <td width="44%" align="left">密码: <input name="password" type="password" class="inputText" id="password" value="" size="16" /></td> </tr> <tr> <td align="left">验证码: <input name="key" type="text" class="inputText" size="10" /> <img src="/e/ShowKey/?v=pl" align="absmiddle" /> </td> <td align="left"><input name="nomember" type="checkbox" id="nomember" value="1" checked="checked" /> 匿名发表</td> </tr> </table> <textarea name="saytext" cols="70" rows="6" id="saytext"></textarea><input name="imageField" type="image" src="/e/data/images/postpl.gif"/> <input name="id" type="hidden" id="id" value="752109" /> <input name="classid" type="hidden" id="classid" value="45" /> <input name="enews" type="hidden" id="enews" value="AddPl" /> <input name="repid" type="hidden" id="repid" value="0" /> <input type="hidden" name="ecmsfrom" value="/wen/2020/03-23/752109.html"></td> </tr> </table> </td> </tr> </table></form> <!-- 评论 --> </div> <div class="mygt"> <div class="mygt_title"> <div class="tcleft"><B>学习交流</B></div> <div class="tcright"><a href="/ge/115/" target="_blank">更多</a></div> </div> <div class="mygt_left"> <ul> <!----> <li><a href="/wen/2020/03-10/743927.html" target="_blank"><img src="/d/file/p/2020/03-10/aaa306be041ca3ecab5f112e4b0516ad.jpg"></a><p> <a href="/wen/2020/03-10/743927.html" target="_blank">MBR怎么转换为GPT?硬盘MBR格式</a></p></li> <!----> <li><a href="/wen/2020/03-10/743925.html" target="_blank"><img src="/d/file/p/2020/03-10/2cfb47f84d3a68d12b13272486fbf421.jpg"></a><p> <a href="/wen/2020/03-10/743925.html" target="_blank">威刚发布新一代Ultimate SU900</a></p></li> <!----> </ul> </div> <div class="mygt_right"> <div class="mygt_bt"> <!----> <p><b><a href="/wen/2020/03-10/743927.html" target="_blank">MBR怎么转换为GPT?硬盘MBR格式转换成GPT格</a></b></p> <span><a href="/wen/2020/03-10/743927.html" target="_blank">MBR怎么转换为GPT?硬盘MBR格式转换成GPT格式教程...</a></span> <!----> </div> <div class="mygt_xt"></div> <ul> <!----> <li><a href="/wen/2019/12-26/620796.html" target="_blank">快剪辑自带水印可以消除,只需简单设置</a><span>12-24</span></li> <!----> <li><a href="/wen/2019/12-26/620795.html" target="_blank">飞鸽传书好友列表显示异常?或许是这两个原因</a><span>12-24</span></li> <!----> <li><a href="/wen/2019/12-26/620794.html" target="_blank">下载网页中视频的方法~~</a><span>12-24</span></li> <!----> <li><a href="/wen/2019/12-26/620793.html" target="_blank">教你用拼音打出来不认识的字~~</a><span>12-24</span></li> <!----> </ul> <ul> <!----> <li><a href="/wen/2020/03-23/753822.html" target="_blank">Nginx服务器上安装并配置PHPMyAdmin的教程</a><span>03-22</span></li> <!----> <li><a href="/wen/2020/03-23/753570.html" target="_blank">Mac配置虚拟主机详细过程</a><span>03-22</span></li> <!----> <li><a href="/wen/2020/03-23/753477.html" target="_blank">关于集群、分布式和负载均衡的区别有哪些?(</a><span>03-22</span></li> <!----> <li><a href="/wen/2020/03-23/752304.html" target="_blank">phpMyAdmin4.4.10安装</a><span>03-22</span></li> <!----> <li><a href="/wen/2020/03-23/752208.html" target="_blank">phpmyadmin导入数据最大限制2048KB的解决方</a><span>03-22</span></li> <!----> </ul> </div> </div> <script src=/d/js/acmsd/gao22.js></script> <div class="pic"> <div class="mygt_title"> <div class="tcleft"><B>热门图片</B></div> <div class="tcright"><a href="/ge/151/" target="_blank">更多</a></div> </div> <ul> <!----> <li><a href="/wen/2020/01-17/639830.html" target="_blank"><img src="/d/file/p/2020/01-17/b4f7716b6b1865b887ea625962938113.jpg"></a><p> <a href="/wen/2020/01-17/639830.html" target="_blank">校园甜美的背影,洋溢着青春烂漫的回忆</a></p></li> <!----> <li><a href="/wen/2019/09-13/2601.html" target="_blank"><img src="/d/file/p/2019/09-13/5513d58eaea5dc8f505cee6cd2ca3406.jpg"></a><p> <a href="/wen/2019/09-13/2601.html" target="_blank">芭蕾舞蹈表演,真实美到极致</a></p></li> <!----> <!----> <li style="margin-right:0px;"><a href="/wen/2020/02-24/719608.html" target="_blank"><img src="/d/file/p/2020/02-24/4dc71d83ad19abfc3bfe10263f3bea68.jpg"></a><p> <a href="/wen/2020/02-24/719608.html" target="_blank">春天的魅力:绿杨烟外晓寒轻</a></p></li> <!----> <li style="margin-right:0px;"><a href="/wen/2020/01-17/639829.html" target="_blank"><img src="/d/file/p/2020/01-17/ff23f28614cb618560c4918c981ad1b8.jpg"></a><p> <a href="/wen/2020/01-17/639829.html" target="_blank">春节临近,各地春节彩灯高高挂</a></p></li> <!----> <!----> <li><a href="/wen/2019/09-16/2621.html" target="_blank"><img src="/d/file/p/2019/09-16/1f46d1c479a54a8333794039c5b0cbf2.jpg"></a><p> <a href="/wen/2019/09-16/2621.html" target="_blank">肉食主义者的最爱美食烤肉图片</a></p></li> <!----> <li><a href="/wen/2019/09-16/2620.html" target="_blank"><img src="/d/file/p/2019/09-16/353e4f9d66694b7652859d454ee26466.jpg"></a><p> <a href="/wen/2019/09-16/2620.html" target="_blank">夏日甜心草莓美食图片</a></p></li> <!----> <!----> <li style="margin-right:0px;"><a href="/wen/2019/09-17/2631.html" target="_blank"><img src="/d/file/p/2019/09-17/109deb9c688e62ab7ec5b71420d1b308.jpg"></a><p> <a href="/wen/2019/09-17/2631.html" target="_blank">人逢知己千杯少,喝酒搞笑图集</a></p></li> <!----> <li style="margin-right:0px;"><a href="/wen/2019/09-17/2630.html" target="_blank"><img src="/d/file/p/2019/09-17/b8e3fc604e37276489853fcc548e6d5f.jpg"></a><p> <a href="/wen/2019/09-17/2630.html" target="_blank">搞笑试卷,学生恶搞答题</a></p></li> <!----> <div style="clear:both"></div> </ul> </div> <script src=/d/js/acmsd/gao23.js></script> <div class="ph"> <div class="ph_left"> <div class="ph_title"><b>猜你喜欢的新闻</b></div> <ul> <!----> <li><a href="/wen/2020/03-22/747136.html" target="_blank">美团云5月31日起停止对用户服务</a></li> <!----> <li><a href="/wen/2020/03-19/746329.html" target="_blank">中国移动2019年净利润1066亿元 超出预期</a></li> <!----> <li><a href="/wen/2020/03-18/746323.html" target="_blank">国内油价重回“5元时代” 92号汽油每升下调</a></li> <!----> <li><a href="/wen/2020/03-18/746322.html" target="_blank">腾讯在天津成立新公司 斥资2亿元人民币</a></li> <!----> <li><a href="/wen/2020/03-18/746321.html" target="_blank">软银集团或放弃收购WeWork 30亿美元收购要</a></li> <!----> <li><a href="/wen/2020/03-18/746318.html" target="_blank">全球十大IC设计公司最新排名</a></li> <!----> <li><a href="/wen/2020/03-15/745091.html" target="_blank">光刻机霸主阿斯麦(ASML):控制芯片产业链上游</a></li> <!----> <li><a href="/wen/2020/03-15/744480.html" target="_blank">博通状告Netflix侵犯专利:电视没人看,我们受</a></li> <!----> <li><a href="/wen/2020/03-14/744059.html" target="_blank">比尔·盖茨退出公司董事会 将继续担任公司</a></li> <!----> <li><a href="/wen/2020/03-13/744049.html" target="_blank">微软被曝高危漏洞“永恒之黑” 或波及全球1</a></li> <!----> </ul> </div> <div class="ph_left"> <div class="ph_title"><b>猜你喜欢的关注</b></div> <ul> <!----> <li><a href="/wen/2020/03-23/754322.html" target="_blank">php的static变量的介绍</a></li> <!----> <li><a href="/wen/2020/03-23/754321.html" target="_blank">PHP中的魔术常量是什么</a></li> <!----> <li><a href="/wen/2020/03-23/754320.html" target="_blank">PHP数组排序函数合集 以及它们之间的联系分</a></li> <!----> <li><a href="/wen/2020/03-23/754319.html" target="_blank">php实现仿写CodeIgniter的购物车类</a></li> <!----> <li><a href="/wen/2020/03-23/754318.html" target="_blank">php.ini 配置文件的深入解析</a></li> <!----> <li><a href="/wen/2020/03-23/754316.html" target="_blank">PHP怎样用正则抓取页面中的网址</a></li> <!----> <li><a href="/wen/2020/03-23/754317.html" target="_blank">Zend Framework教程之Zend_Config_Xml用法</a></li> <!----> <li><a href="/wen/2020/03-23/754315.html" target="_blank">3个PHP多维数组转为一维数组的方法实例</a></li> <!----> <li><a href="/wen/2020/03-23/754314.html" target="_blank">PHP把网页保存为word文件的三种方法</a></li> <!----> <li><a href="/wen/2020/03-23/754313.html" target="_blank">对于PHP的Yii框架中的Controller控制器的解</a></li> <!----> </ul> </div> </div> </div> <div class="content1_right"> <div class="right_ad1"> <!--广告 --> <script src=/d/js/acmsd/gao24.js></script> <!--广告 --> </div> <div class="right_001"> <div class="title2"> <p class="p1">新闻热点</p> </div> <!-- --> <div class="xwt2"> <div class="xwt2_left"><a href="/wen/2020/03-22/747136.html" target="_blank"><img src="/d/file/p/2020/03-22/810c694dbaec90f84137c1b9f82bb604.png"></a></div> <div class="xwt2_right"> <div class="xwt2_right_a"><a href="/wen/2020/03-22/747136.html" target="_blank">美团云5月31日起停止对用户服务</a></div> <div class="xwt2_right_b">2020-03-22 13:00:36</div> </div> </div> <!-- --> <div class="xwt2"> <div class="xwt2_left"><a href="/wen/2020/03-19/746329.html" target="_blank"><img src="/d/file/p/2020/03-19/4dbcf573c84c207ad9dee2ef0b0ba1a9.jpg"></a></div> <div class="xwt2_right"> <div class="xwt2_right_a"><a href="/wen/2020/03-19/746329.html" target="_blank">中国移动2019年净利润1066亿元 超出预期</a></div> <div class="xwt2_right_b">2020-03-19 18:53:05</div> </div> </div> <!-- --> <div class="xwt2"> <div class="xwt2_left"><a href="/wen/2020/03-18/746323.html" target="_blank"><img src="/d/file/p/2020/03-18/a28b748ad37024e8a409a732be6b7b71.jpg"></a></div> <div class="xwt2_right"> <div class="xwt2_right_a"><a href="/wen/2020/03-18/746323.html" target="_blank">国内油价重回“5元时代” 92号汽油每升下调0.80元</a></div> <div class="xwt2_right_b">2020-03-18 19:35:57</div> </div> </div> <!-- --> <div class="xwt2"> <div class="xwt2_left"><a href="/wen/2020/03-18/746322.html" target="_blank"><img src="/d/file/p/2020/03-18/e285a6e5ca5f1ad6e2b2ed422f1201e2.png"></a></div> <div class="xwt2_right"> <div class="xwt2_right_a"><a href="/wen/2020/03-18/746322.html" target="_blank">腾讯在天津成立新公司 斥资2亿元人民币</a></div> <div class="xwt2_right_b">2020-03-18 19:32:46</div> </div> </div> <!-- --> <div class="xwt2"> <div class="xwt2_left"><a href="/wen/2020/03-18/746321.html" target="_blank"><img src="/d/file/p/2020/03-18/26643688c9fd0807f26cb86444c39b12.jpg"></a></div> <div class="xwt2_right"> <div class="xwt2_right_a"><a href="/wen/2020/03-18/746321.html" target="_blank">软银集团或放弃收购WeWork 30亿美元收购要约计划告吹</a></div> <div class="xwt2_right_b">2020-03-18 19:30:20</div> </div> </div> <!-- --> <div class="xwt2"> <div class="xwt2_left"><a href="/wen/2020/03-18/746318.html" target="_blank"><img src="/d/file/p/2020/03-18/2e434dcc8b8f1b14cb2d34359de0b566.jpg"></a></div> <div class="xwt2_right"> <div class="xwt2_right_a"><a href="/wen/2020/03-18/746318.html" target="_blank">全球十大IC设计公司最新排名</a></div> <div class="xwt2_right_b">2020-03-18 13:04:38</div> </div> </div> <!-- --> </div> <div class="right_ad1"> <!--广告 --> <script src=/d/js/acmsd/gao25.js></script> <!--广告 --> </div> <div class="right_001"> <div class="title2"> <p class="p1">疑难解答</p> </div> <div class="new3"> <ul> <!-- --> <li><a href="/wen/2020/03-10/743927.html" target="_blank">MBR怎么转换为GPT?硬盘MBR格式转换成GPT格式</a></li> <!-- --> <li><a href="/wen/2020/03-10/743926.html" target="_blank">SSD干货 固态硬盘选购指南</a></li> <!-- --> <li><a href="/wen/2020/03-10/743925.html" target="_blank">威刚发布新一代Ultimate SU900固态硬盘:罕</a></li> <!-- --> <li><a href="/wen/2020/03-10/743924.html" target="_blank">三星发布容量达32TB的新硬盘 能效更高</a></li> <!-- --> <li><a href="/wen/2020/03-10/743923.html" target="_blank">美光Crucial MX300 SSD新增三款型号:奇怪的</a></li> <!-- --> <li><a href="/wen/2020/03-10/743922.html" target="_blank">电脑硬盘蓝盘黑盘红盘绿盘选择推荐</a></li> <!-- --> <li><a href="/wen/2020/03-10/743921.html" target="_blank">GPT分区格式怎么转成MBR 硬盘GPT和MBR分区</a></li> <!-- --> <li><a href="/wen/2020/03-10/743920.html" target="_blank">电脑更新后系统不识别移动硬盘的原因分析及</a></li> <!-- --> <li><a href="/wen/2020/03-10/743919.html" target="_blank">威刚推出入门级XPG SX7000系列M.2 SSD</a></li> <!-- --> <li><a href="/wen/2020/03-10/743918.html" target="_blank">2TGB大容量金士顿DataTraveler Ultimate GT</a></li> <!-- --> </ul> </div> </div> <div class="right_001"> <div class="title2"> <p class="p1">图片精选</p> </div> <div class="new4"> <ul> <!-- --> <li class="m_R"><a href="/wen/2020/03-23/754304.html" target="_blank"><img src="/d/file/p/2020/03-23/d9a164719654cc6d73bf1a95cd0bc532.jpg"><p>自己搭建一个 Laravel 的 Docker的</p></a></li> <!-- --> <!-- --> <li><a href="/wen/2020/03-23/754284.html" target="_blank"><img src="/d/file/p/2020/03-23/f6d71c2ebc4f2d553ef97ba86d829279.png"><p>yii2权限控制rbac之rule详细讲解</p></a></li> <!-- --> <!-- --> <li class="m_R"><a href="/wen/2020/03-23/754264.html" target="_blank"><img src="/d/file/p/2020/03-23/3bbe4051970e5b95d481983a923e65e4.jpg"><p>php如何实现上篇和下篇的功能(代码)</p></a></li> <!-- --> <!-- --> <li><a href="/wen/2020/03-23/754247.html" target="_blank"><img src="/d/file/p/2020/03-23/5ae03c0b9775040faf35e86834a93d2c.png"><p>PHP下扩展memcache模块</p></a></li> <!-- --> </ul> </div> </div> <div class="right_ad1"> <!--广告 --> <script src=/d/js/acmsd/gao26.js></script> <!--广告 --> </div> <div class="right_001"> <div class="title2"> <p class="p1">网友关注</p> </div> <div class="new5"> <ul> <!-- --> <li><a href="/wen/2019/12-26/620797.html" target="_blank">PPT超强插件打包分享,从此不再被同事吐槽</a></li> <!-- --> <li><a href="/wen/2019/12-26/620796.html" target="_blank">快剪辑自带水印可以消除,只需简单设置</a></li> <!-- --> <li><a href="/wen/2019/12-26/620795.html" target="_blank">飞鸽传书好友列表显示异常?或许是这两个原因</a></li> <!-- --> <li><a href="/wen/2019/12-26/620794.html" target="_blank">下载网页中视频的方法~~</a></li> <!-- --> <li><a href="/wen/2019/12-26/620793.html" target="_blank">教你用拼音打出来不认识的字~~</a></li> <!-- --> <li><a href="/wen/2019/12-26/620792.html" target="_blank">武林网教你设置电脑开机声音教程</a></li> <!-- --> <li><a href="/wen/2019/12-26/620791.html" target="_blank">教你如何检查主板USB功能是否激活</a></li> <!-- --> <li><a href="/wen/2019/12-26/620790.html" target="_blank">启动桌面清理向导的方法教程</a></li> <!-- --> <li><a href="/wen/2019/12-26/620789.html" target="_blank">电脑下载东西经常掉线的解决方法</a></li> <!-- --> <li><a href="/wen/2019/12-26/620788.html" target="_blank">修改BIOS 避免误触Power键造成关机</a></li> <!-- --> </ul> </div> </div> </div> <div style="text-align:center;"><script src=/d/js/acmsd/gao27.js></script></div> <div style="clear:both;"></div> </div> <div style="clear:both;"></div> <!--footer--> <div class="footer"> <div class="bq"><a href="/d/about.html" target="_blank">关于本站</a> - <a href="/d/upload.html" target="_blank">网上投稿</a> - <a href="/d/business.html" target="_blank">商务合作</a> - <a href="/d/legal.html" target="_blank">隐私政策</a> - <a href="/d/sitemap.html" target="_blank">网站地图</a></div> <div class="bq"></div> <div class="bq"> Copyright © 2008 - 2020 VEVB.COM. All Rights Reserved.武林网 版权所有 </div> </div> <script src="/d/t.js"></script> <!--footer end--> <script src="/e/public/onclick/?enews=donews&classid=45&id=752109"></script> </body> </html>