ホームページ >php教程 >php手册 >PHPでの漢字変換:GBK->Unicode(UTF8)

PHPでの漢字変換:GBK->Unicode(UTF8)

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBオリジナル
2016-06-21 09:06:13870ブラウズ

漢字|変換

PHPの漢字変換は昔から面倒でした

このクラスには「htmlHex」、「htmlDec」、「escape」、「u2utf8」の4つの組み込み関数があります
ユーザーが使うのに便利で、機能もカスタマイズ可能です 好きなようにしてください

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=""){
/* *****(秋水烏編 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("無効な関数");
$ret.=$this->$arr[$callback-1]($q);
}else
$ret.=$callback($ q);
}
}
return $ret;
}

function htmlHex($str){
return "&#x".$str.";";
}

function htmlDec($str){
return "&# ".hexdec($str).";";
}

functionscape($str){
return hexdec($str)<256?chr(hexdec($str)):"%u ".$str ;
}

function u2utf8($str){
/******(秋水烏編 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="%".strto lower($str);
}
return $ret;
}
}

使用例
$words="中国語Abc";
function ex($str){ return "[" .$str."]";}


$qswh=new qswhGBK("qswhGBK.php");//ファイル名がqswhGBK.phpの場合パラメータは省略可能

echo( "

いいえ パラメーターあり: ".$qswh->gb2u($words));<br/>echo("n組み込み関数 htmlHex を呼び出します:".$qswh->gb2u($words,1)) ;<br/>echo("n 組み込み関数 Function htmlDec を呼び出します:".$qswh->gb2u($words,2));<br>echo("n 組み込み関数scape を呼び出します:".$qswh-> gb2u($words,3));<br>echo("n 組み込み関数 u2utf8 を呼び出します:".$qswh->gb2u($words,4));<br>echo("n カスタム関数を呼び出します:".$ qswh->gb2u($words,ex));</font></p> <p>効果は次のとおりです: </p> <p>パラメータなし: 4E2D6587416263 は組み込み関数 htmlHex を呼び出します: 中国語 Abc は組み込み関数を呼び出します htmlDec: 中国語 Abc は組み込み関数を呼び出します関数エスケープ:%u4E2D%u6587Abc は組み込み関数 u2utf8:%e4%b8%ad%e6%96%87Abc を呼び出しますカスタム関数:[4E2D][6587][41][62][63]<br></p> <p style="width:100%;text-align:center;margin:10px 0"> <br> <br> </p> <p style="width:100%;text-align:center;margin:10px 0"> </p> <p class="clear"></p></div><div class="nphpQianMsg"><div class="clear"></div></div><div class="nphpQianSheng"><span>声明:</span><div>この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。</div></div></div><div class="nphpSytBox"><span>前の記事:<a class="dBlack" title="画像に透かしを追加する PHP クラシック プログラム" href="https://m.php.cn/ja/faq/239559.html">画像に透かしを追加する PHP クラシック プログラム</a></span><span>次の記事:<a class="dBlack" title="画像に透かしを追加する PHP クラシック プログラム" href="https://m.php.cn/ja/faq/239564.html">画像に透かしを追加する PHP クラシック プログラム</a></span></div><div class="nphpSytBox2"><div class="nphpZbktTitle"><h2>関連記事</h2><em><a href="https://m.php.cn/ja/article.html" class="bBlack"><i>続きを見る</i><b></b></a></em><div class="clear"></div></div><ins class="adsbygoogle" style="display:block" data-ad-format="fluid" data-ad-layout-key="-6t+ed+2i-1n-4w" data-ad-client="ca-pub-5902227090019525" data-ad-slot="8966999616"></ins><script> (adsbygoogle = window.adsbygoogle || []).push({}); </script><ul class="nphpXgwzList"><li><b></b><a href="https://m.php.cn/ja/faq/92233.html" title="Windows7系统下Netbeans+PHPUnit搭建PHP单元测试开发环境及PHPUn" class="aBlack">Windows7系统下Netbeans+PHPUnit搭建PHP单元测试开发环境及PHPUn</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/ja/faq/97151.html" title="程序猿ProMonkey V2.03" class="aBlack">程序猿ProMonkey V2.03</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/ja/faq/94770.html" title="PHP Navigator" class="aBlack">PHP Navigator</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/ja/faq/90182.html" title="php返回数组中指定的一列(php5.5.0默认函数array_column()在php" class="aBlack">php返回数组中指定的一列(php5.5.0默认函数array_column()在php</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/ja/faq/208396.html" title="htmlentitiesとhtmlspecialcharsの違いの詳しい説明" class="aBlack">htmlentitiesとhtmlspecialcharsの違いの詳しい説明</a><div class="clear"></div></li></ul></div></div><ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5902227090019525" data-ad-slot="5027754603"></ins><script> (adsbygoogle = window.adsbygoogle || []).push({}); </script><footer><div class="footer"><div class="footertop"><img src="/static/imghwm/logo.png" alt=""><p>福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!</p></div><div class="footermid"><a href="https://m.php.cn/ja/about/us.html">私たちについて</a><a href="https://m.php.cn/ja/about/disclaimer.html">免責事項</a><a href="https://m.php.cn/ja/update/article_0_1.html">Sitemap</a></div><div class="footerbottom"><p> © php.cn All rights reserved </p></div></div></footer><script>isLogin = 0;</script><script type="text/javascript" src="/static/layui/layui.js"></script><script type="text/javascript" src="/static/js/global.js?4.9.47"></script></div><script src="https://vdse.bdstatic.com//search-video.v1.min.js"></script><link rel='stylesheet' id='_main-css' href='/static/css/viewer.min.css' type='text/css' media='all'/><script type='text/javascript' src='/static/js/viewer.min.js?1'></script><script type='text/javascript' src='/static/js/jquery-viewer.min.js'></script><script>jQuery.fn.wait = function (func, times, interval) { var _times = times || -1, //100次 _interval = interval || 20, //20毫秒每次 _self = this, _selector = this.selector, //选择器 _iIntervalID; //定时器id if( this.length ){ //如果已经获取到了,就直接执行函数 func && func.call(this); } else { _iIntervalID = setInterval(function() { if(!_times) { //是0就退出 clearInterval(_iIntervalID); } _times <= 0 || _times--; //如果是正数就 -- _self = $(_selector); //再次选择 if( _self.length ) { //判断是否取到 func && func.call(_self); clearInterval(_iIntervalID); } }, _interval); } return this; } $("table.syntaxhighlighter").wait(function() { $('table.syntaxhighlighter').append("<p class='cnblogs_code_footer'><span class='cnblogs_code_footer_icon'></span></p>"); }); $(document).on("click", ".cnblogs_code_footer",function(){ $(this).parents('table.syntaxhighlighter').css('display','inline-table');$(this).hide(); }); $('.nphpQianCont').viewer({navbar:true,title:false,toolbar:false,movable:false,viewed:function(){$('img').click(function(){$('.viewer-close').trigger('click');});}}); </script></body><!-- Matomo --><script> var _paq = window._paq = window._paq || []; /* tracker methods like "setCustomDimension" should be called before "trackPageView" */ _paq.push(['trackPageView']); _paq.push(['enableLinkTracking']); (function() { var u="https://tongji.php.cn/"; _paq.push(['setTrackerUrl', u+'matomo.php']); _paq.push(['setSiteId', '9']); var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s); })(); </script><!-- End Matomo Code --></html>