Php Chinese character conversion: GBK to Big5_PHP tutorial

Source: Internet
Author: User
Conversion of php Chinese characters: GBK to Big5. Pphp's conversion of Chinese characters has always been quite troublesome. First, we should say two points: 1. the traditional Chinese version of GBK is not Big5, but generally the traditional Chinese version has a corresponding Big Code 2. a gbk code does not necessarily have a corresponding Big5 code P> Chinese character conversion in php has always been a hassle
There are two points:
1. GBK traditional Chinese characters are not Big5, but generally traditional Chinese characters have Big codes.
2. a GBK code does not necessarily have a corresponding Big5 code, so it is generally irreversible conversion.
This class has three built-in functions.
QswhStr format string
QswhFile format a file
QswhDir format all files in the Directory (except for. qswh, which is the backup of the original document)
QswhBig5.php download from here
Http://www.blueidea.com/user/qswh/qswhBig5.zip
Class qswhBig5 {
Var $ qswhData;
Function qswhBig5 ($ filename = "qswhBig5.php "){
$ This-> qswhData = file ($ filename );
}
Function qswhStr ($ gb, $ fail = "?? "){
/***** (Qiushuiwuhen 2002-9-6 )******/
$ Ret = "";
For ($ I = 0; $ I If ($ p = ord (substr ($ gb, $ I, 1)> 127 ){
$ Q = ord (substr ($ gb, ++ $ I, 1 ));
$ Tmp = $ this-> qswhData [$ p-128];
For ($ j = 0; $ j = $ Q) break;
If ($ k = $ q) $ q = chr (hexdec (substr ($ tmp, $ j + 2, 2 ))). chr (hexdec (substr ($ tmp, $ j + 4, 2 )));
Else if ($ fail = "") $ q = chr ($ p). chr ($ q); else $ q = $ fail;
}
Else
$ Q = chr ($ p );
$ Ret. = $ q;
}
Return $ ret;
}
Function qswhFile ($ filename, $ fail = "?? "){
/***** (Qiushuiwuhen 2002-9-6 )******/
If (! File_exists ($ filename. ". qswh") copy ($ filename, $ filename. ". qswh ");
$ Fp = fopen ($ filename, "r + ");
$ Tmp = fread ($ fp, filesize ($ filename ));
Rewind ($ fp );
Fwrite ($ fp, $ this-> qswhStr ($ tmp, $ fail ));
Fclose ($ fp );
}

First, let's talk about two points: 1. the traditional Chinese characters of GBK are not Big5, but generally the traditional Chinese characters have the corresponding Big Code. 2. a GBK code does not necessarily have the corresponding Big5 code...

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.