Conversion of Chinese characters to PHP: Gbk->big5

Source: Internet
Author: User
Tags chr ord php download strlen
Chinese Character | conversion

The conversion of Chinese characters in PHP has always been a tricky business.

First of all, two points:
1.GBK traditional characters are not Big5, but the general characters have corresponding big code
2. A GBK code does not necessarily have a corresponding BIG5 code, so it is generally irreversible change

This class has three functions built into it
QSWHSTR format string
Qswhfile format a file
Qswhdir All files (except the. QSWH, which is the backup of the original document) under the format directory

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 <strlen ($GB); $i + +) {
if ($p =ord (substr ($GB, $i, 1)) >127) {
$q =ord (substr ($GB, + + $i, 1));
$tmp = $this->qswhdata[$p-128];
for ($j =0; $j <strlen ($tmp); $j +=6) if (($k =hexdec (substr ($tmp, $j, 2)) >= the $q) break;
if ($k = = $q) $q =chr (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;
}

}


Use Example:
$words = "The eyes have no hate";
$QSWH =new qswhBig5 ("qswhbig5.php");//qswhbig5.php, can be province parameter

Echo ("<xmp> Select encoding Big5 view:". $qswh->qswhstr ($words));
Echo ("\ n encode a directory:". $QSWH->qswhdir ("test"));
Echo ("\ n encode a file:". $qswh->qswhfile ("index.htm"));


Instructions for use:
The second parameter in QSWHSTR represents the word if no corresponding Big5 will be substituted, and if it is empty, the word is preserved
After formatting the file, remember to change the Content-type in <meta to big.



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.