Home > Backend Development > PHP Tutorial > PHP Chinese character conversion GBK-UnicodeUTF8_PHP tutorial

PHP Chinese character conversion GBK-UnicodeUTF8_PHP tutorial

WBOY
Release: 2016-07-13 17:25:36
Original
774 people have browsed it


PHP Chinese character conversion has always been a troublesome thing
This class has four built-in functions "htmlHex", "htmlDec", "escape", "u2utf8"
It is convenient for users to use, and it can also be customized Define functions to perform your favorite operations
qswhGBK.php Download from here
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 "".$str.";";
}
function htmlDec($str){
return "".hexdec($str).";";
}
function escape($str){
return hexdec($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;
}
}
Usage example
$words ="Chinese Abc";
function ex($str){return "[".$str."]";}
$qswh=new qswhGBK("qswhGBK.php");//If the file name It is qswhGBK.php, you can omit the parameter echo("Without parameters:".$qswh->gb2u($words));echo(" Call the built-in function htmlHex: ".$qswh->gb2u($words,1));echo(" Call the built-in function htmlDec:".$qswh->gb2u($words,2));echo(" Call the built-in function escape:".$qswh->gb2u($words,3));echo(" Call the built-in function u2utf8:".$qswh->gb2u($words,4));echo(" Call the custom function: ".$qswh->gb2u($words,ex)); The effect is as follows: without parameters: 4E2D6587416263 Call the built-in function htmlHex: Chinese Abc calls the built-in function htmlDec: Chinese Abc calls the built-in function escape:%u4E2D% u6587Abc calls the built-in function u2utf8:%e4%b8%ad%e6%96%87Abc calls the custom function: [4E2D][6587][41][62][63]ps. This article is now only available in the csdn Document Center, If you want to reprint, please contact the author, otherwise you will be responsible for the consequences

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532056.htmlTechArticlePHP Chinese character conversion has always been a troublesome thing. This class has four built-in functions "htmlHex", "htmlDec" ,"escape","u2utf8" is convenient for users to use. At the same time, you can also customize the function to do what you like...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template