Conversion of php Chinese characters: GBK to Unicode (UTF8)

Source: Internet
Author: User
The conversion of php Chinese characters has always been quite troublesome. This class has four built-in functions: htmlHex, htmlDec, escape, and u2utf8 for your convenience. you can also customize functions to perform your favorite operations on qswhGBK. php download http://www.blueidea.com/user/qswh/qswhGBK.zip from here? ClassqswhGBK {var $

Converting Chinese characters in php has always been a problem.

This class has four built-in functions: "htmlHex", "htmlDec", "escape", "u2utf8"
It is convenient for you to use, and you can also customize functions for 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 "& # 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;
}
}


Example

$ Words = "Chinese Abc ";
Function ex ($ str) {return "[". $ str. "]";}


$ Qswh = new qswhGBK ("qswhGBK. php"); // if the file name is qswhGBK. php, save the parameter

Echo (" Without parameters :". $ qswh-&gt; gb2u ($ words); &lt;br/&gt; echo ("\ n calls the built-in function htmlHex :". $ qswh-&gt; gb2u ($ words, 1); &lt;br/&gt; echo ("\ n calls the built-in function htmlDec :". $ qswh-&gt; gb2u ($ words, 2); &lt;br/&gt; echo ("\ n call the built-in function escape :". $ qswh-&gt; gb2u ($ words, 3); &lt;br/&gt; echo ("\ n call the built-in function u2utf8 :". $ qswh-&gt; gb2u ($ words, 4); &lt;br/&gt; echo ("\ n call a UDF :". $ qswh-&gt; gb2u ($ words, ex); &lt;br/&gt; &lt;p&gt; &lt;br/&gt; The effect is as follows: &lt;br/&gt; without parameters: 4E2D6587416263 &lt;br/&gt; call the built-in function htmlHex: &amp; # x4E2D; &amp; # x6587; &amp; # x41; &amp; # x62; &amp; # x63; &lt;br/&gt; call the built-in function htmlDec: &amp; #20013; &amp; #25991; &amp; #65; &amp; #98; &amp; #99; &lt;br/&gt; call the built-in function escape: % u4E2D % u6587Abc &lt;br/&gt; call the built-in function u2utf8: % e4 % b8 % ad % e6 % 96% 87Abc &lt;br/&gt; call the UDF: [4E2D] [6587] [41] [62] [63] &lt;br/&gt; &lt;/p&gt;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.