程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php的漢字轉換GBK-UnicodeUTF8

php的漢字轉換GBK-UnicodeUTF8

編輯:關於PHP編程


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文檔中心,如要轉載,請和作者聯系,否則後果自負。 </p> <div class="qcxs_ie_he"></div> </div> <div class="akevx_own"><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-4480737146802772" crossorigin="anonymous"></script> <!-- aspphp 2 --> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-4480737146802772" data-ad-slot="9530743267" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script></div> <div class="akevx_mj"></div> <div class="qcxs_ie_he"></div> <ol class="qmsbvx_hsb ovv_hex"> <li>上一頁:<a class='LinkPrevArticle' href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/150572.html' >php的漢字轉換GBK-Big5</a></li><li>下一頁:<a class='LinkNextArticle' href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/150574.html' >使用PHP4中的IntegratedTemplate類實現HTML和PHP代碼分離</a></li> </ol> </div> </div> <div class="qirnxbd bg1 mt20"> <div class="jdbsbav"> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/' target="_blank">關於PHP編程</a> <div class="jdbsbav_hx"></div> </div> <div class="qirnxbd_rbx"> <div class="oiendx"> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/139734.html' target="_blank"><img class='pic1' src='https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011617302029.png' width='100' height='70' border='0'></a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/139734.html' target="_blank"><strong>PHP實現批量上傳單個文件,</strong></a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/139734.html' target="_blank"><p>PHP實現批量上傳單個文件,很多時候當我們通過某個通用型RC</p></a> </div> <div class="oiendx"> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/146718.html' target="_blank"><img class='pic1' src='https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011711405369_S.jpg' width='100' height='70' border='0'></a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/146718.html' target="_blank"><strong>8 個最佳 PHP 庫</strong></a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/146718.html' target="_blank"><p>PHP標准庫 (SPL)的目的就是提供一組接口,讓開發者在P</p></a> </div> <div class="oiendx"> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/143902.html' target="_blank"><img class='pic1' src='https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709490037_S.jpg' width='100' height='70' border='0'></a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/143902.html' target="_blank"><strong>用 Composer構建自己的 PHP 框架之使用 ORM,composerorm</strong></a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/143902.html' target="_blank"><p>用 Composer構建自己的 PHP 框架之使用 ORM,</p></a> </div> <div class="oiendx"> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/142870.html' target="_blank"><img class='pic1' src='https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011709313048_S.jpg' width='100' height='70' border='0'></a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/142870.html' target="_blank"><strong>php創建、獲取cookie及基礎要點分析,phpcookie</strong></a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/142870.html' target="_blank"><p>php創建、獲取cookie及基礎要點分析,phpcooki</p></a> </div> <div class="oiendx"> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/136581.html' target="_blank"><img class='pic1' src='https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011614572769.png' width='100' height='70' border='0'></a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/136581.html' target="_blank"><strong>ddchuxing——php面試題總結,ddchuxingphp試題</strong></a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/136581.html' target="_blank"><p>ddchuxing——php面試題總結,ddchuxingp</p></a> </div> <div class="oiendx"> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/137747.html' target="_blank"><img class='pic1' src='https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011616012195.png' width='100' height='70' border='0'></a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/137747.html' target="_blank"><strong>Centos PHP 擴展Xchche的安裝教程,centosxchche</strong></a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/137747.html' target="_blank"><p>Centos PHP 擴展Xchche的安裝教程,cento</p></a> </div> <div class="qcxs_ie_he"></div> </div> </div> </div> <div class="eccsgxsg mt20"> <div class="xiangbgbax bg1"> <div class="ejmnxbns">相關文章</div> <ul class="pdirbvx lbhevs ovv_hex lbhevs_40"> <li><a class='LinkArticleCorrelative' href='https://www.aspphp.online/bianchen/wangye/php/phpzh/201704/231982.html' target="_self">PHP獲取用戶的真實IP地址</a></li><li><a class='LinkArticleCorrelative' href='https://www.aspphp.online/bianchen/wangye/php/phpzh/201704/231981.html' target="_self">centos&nbsp;7下如何定時執行php腳本</a></li><li><a class='LinkArticleCorrelative' href='https://www.aspphp.online/bianchen/wangye/php/phpzh/201704/231980.html' target="_self">php操作mysql數據庫的方式</a></li><li><a class='LinkArticleCorrelative' href='https://www.aspphp.online/bianchen/wangye/php/phpzh/201704/231969.html' target="_self">PHP基礎學習</a></li><li><a class='LinkArticleCorrelative' href='https://www.aspphp.online/bianchen/wangye/php/phpzh/201704/231964.html' target="_self">php正則表達式</a></li><li><a class='LinkArticleCorrelative' href='https://www.aspphp.online/bianchen/wangye/php/phpzh/201704/231963.html' target="_self">PHP如何自動轉義和還原字符串</a></li><li><a class='LinkArticleCorrelative' href='https://www.aspphp.online/bianchen/wangye/php/phpzh/201704/231962.html' target="_self">PHP截取字符串的例子</a></li><li><a class='LinkArticleCorrelative' href='https://www.aspphp.online/bianchen/wangye/php/phpzh/201704/231961.html' target="_self">PHP如何檢測密碼長度</a></li><li><a class='LinkArticleCorrelative' href='https://www.aspphp.online/bianchen/wangye/php/phpzh/201704/231960.html' target="_self">php異常處理</a></li><li><a class='LinkArticleCorrelative' href='https://www.aspphp.online/bianchen/gengduo/delphi/201704/231846.html' target="_self">Delphi7.0常用函數-屬性-事件,delphi7.0函數</a></li> </ul> </div> <div class="akevx_oin"></div> <div class="nenxnxb bg1 mt20"> <div class="enxvsfgg"> <div class="enxvsfgg_1">閱讀排行榜</div> </div> <div class="pdirbvx lbhevs ovv_hex"> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/144124.html' target="_blank">PHP @ at 記號的作用,phpat記號作用</a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/147865.html' target="_blank">php Allowed memory size of 8388608 bytes exhausted (tried to</a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/159062.html' target="_blank">php checkdate、getdate等日期時間函數操作詳解</a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/143243.html' target="_blank">PHP中require和include路徑問題總結,requireinclude</a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/156688.html' target="_blank">深入理解curl類,可用於模擬get,post和curl下載</a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/139218.html' target="_blank">Yii中CArrayDataProvider和CActiveDataProvider區別實例分析,carraydataprovider</a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/139314.html' target="_blank">在Mac OS上編譯安裝Nginx+PHP+MariaDB開發環境的教程,nginxmariadb</a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/141210.html' target="_blank">在Windows系統下使用PHP生成Word文檔的教程,</a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/155059.html' target="_blank">兼容性最強的PHP生成縮略圖的代碼</a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/156151.html' target="_blank">Wglhosting 免費提供10G支持PHP空間</a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/153888.html' target="_blank">PHP數組函數對我們編碼中的作用體現</a> </div> </div> <div class="wuwbxbxsv mt20"> <div class="hrbbxsxsf">熱門圖文</div> <div class="sbnexs"> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/138603.html' target="_blank"><img class='pic1' src='https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011616274779.png' width='230' height='170' border='0'></a> </div> <div class="pdirbvx lbhevs ovv_hex lbhevs_80"> <a href='https://www.aspphp.online/bianchen/dnet/gydnet/201701/169559.html' target="_blank">C#定時執行,</a> <a href='https://www.aspphp.online/bianchen/cyuyan/cjj/cjjrm/201701/211662.html' target="_blank">Essential C++ 學習筆記02--Array/Vector 與指針</a> <a href='https://www.aspphp.online/bianchen/cyuyan/cjj/gycjj/201701/4313.html' target="_blank">NYOJ 737 石子合並(一) (區間DP+平行四邊形優化)</a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/40981.html' target="_blank">PHP動態規劃解決0-1背包問題實例分析</a> <a href='https://www.aspphp.online/bianchen/dnet/dnetsl/201701/106353.html' target="_blank">一個簡單的HOOK API的DLL</a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/143580.html' target="_blank">PHP中shuffle數組值隨便排序函數用法,shuffle數組</a> <a href='https://www.aspphp.online/bianchen/gengduo/bczhwd/201701/32157.html' target="_blank">class-文件上傳加參數files=文件流上傳類型為enctype=multipart/form-data</a> <a href='https://www.aspphp.online/bianchen/wangye/php/gyphp/201701/153467.html' target="_blank">使用PHP處理XML配置文件</a> </div> </div> <div class="eqibneb bg1 mt20"> <div class="lkebxsg">欄目導航</div> <div class="eqibneb_1 ovv_hex"> <a class='childclass' href='https://www.aspphp.online/bianchen/wangye/php/phpjc/' target="_self">PHP基礎知識</a><a class='childclass' href='https://www.aspphp.online/bianchen/wangye/php/phpzh/' target="_self">PHP綜合</a><a class='childclass' href='https://www.aspphp.online/bianchen/wangye/php/phprm/' target="_self">PHP入門知識</a><a class='childclass' href='https://www.aspphp.online/bianchen/wangye/php/gyphp/' target="_self">關於PHP編程</a> </div> </div> </div> <div class="qcxs_ie_he"></div> </div> </div> <div class="qcxs_ie_he"></div> <div class="ebrxbsbf">Copyright &copy; <a href="https://www.aspphp.online/" target="_blank" title="程式師世界">程式師世界</a> All Rights Reserved </div> </body> </html>