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

Source: Internet
Author: User
Tags php download

P> conversion of UTF8 Chinese characters using GBK Unicode
Converting Chinese Characters in php has always been a problem.
This class has four built-in filters "& # [dec];", "& # x [hex];", "% u [hex]", "utf8 conversion"
It is convenient for users and supports custom Filtering for their favorite operations.
QswhU. php download from here
Http://www.blueidea.com/user/qswh/qswhU.zip
Class qswhU {
Var $ qswhData;
Function qswhU ($ filename = "qswhU. php "){
$ This-> qswhData = file ($ filename );
}
 
Function decode ($ str, $ pattern = 0 ){
$ Arr = array ("/& # (w +);/iU", "/(% ww) +)/I ", "/% u (w {4, 5})/iU ");
If (is_integer ($ pattern )){
If ($ pattern> = count ($ arr) die ("Invalid Function ");
$ Pattern = $ arr [$ pattern];
}
Return preg_replace_callback ($ pattern, array ($ this, "u2gb"), $ str );
}
 
Function u2gb ($ arr ){
/***** (Qiushuiwuhen 2002-8-15 )******/
$ Ret = ""; $ str = $ arr [1];
If (preg_match_all ("/% w {2}/", $ str, $ matches )){
For ($ I = 0; $ I <count ($ matches [0]); $ I ++ ){
$ Chr1 = hexdec (substr ($ matches [0] [$ I], 1 ));
$ Arr = array ("f0", "e0", "c0", "0 ");
For ($ j = 0; $ j <count ($ arr); $ j ++) if ($ chr1> hexdec ($ arr [$ j]) break;
$ Chr = hexdec (substr ($ matches [0] [$ I], 1)-hexdec ($ arr [$ j]);
While (++ $ j <count ($ arr) $ chr = $ chr * 0x40 + (hexdec (substr ($ matches [0] [++ $ I], 1)-0x80 );
$ Str = dechex ($ chr );
If (strlen ($ str) = 4 ){
$ P = hexdec (substr ($ str, 0, 2)-0x4d;
$ Q = hexdec (substr ($ str, 2) * 4;

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.