Php Chinese character conversion GBK-Big5_PHP tutorial

Source: Internet
Author: User
Php Chinese character conversion GBK-Big5. The conversion of Chinese characters in php 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. it is always troublesome to convert Chinese characters in php.
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 );
}
Function qswhDir ($ dirname, $ fail = "?? "){
/***** (Qiushuiwuhen 2002-9-6 )******/
$ D = dir ($ dirname );
While ($ entry = $ d-> read ()){
If ($ entry = "." | $ entry = "...") continue;
$ Entry = $ dirname. "/". $ entry;
If (is_dir ($ entry ))
$ This-> qswhDir ($ entry, $ fail );
Else {
If (substr ($ entry,-5 )! = ". Qswh") $ this-> qswhFile ($ entry, $ fail );
}
}
$ D-> close ();
Return $ c;
}
}
Example:
$ Words = "no hate in autumn ";
$ Qswh = new qswhBig5 ("qswhBig5.php"); // qswhBig5.php, which saves parameters
Echo (" Please select the encoding Big5 to view :". $ qswh-> qswhStr ($ words); <br> echo ("encoding 1 Directory :". $ qswh-> qswhDir ("test"); <br> echo ("encode a File :". $ qswh-> qswhFile ("index.htm"); <br> Usage: <br> The second parameter in qswhStr indicates the word that will be replaced if no corresponding Big5 is found, if it is empty, retain the word <br>. after formatting the file, remember to modify the content-type in <meta to Big. Ps. This article is only available in the csdn document center. if you want to reprint it, please contact the author; otherwise, the consequences will be borne by you. </P> <p align = "left"> <p style = "display: none;"> The first two points of attention are: 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> <p class = "art_confoot">

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.