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.