Coding
/******************************
UTF-8 GB Encoding
*******************************/
function UTF82GB ($UTFSTR)
{
Global $UC 2GBTABLE;
$okstr = "";
if (Trim ($utfstr) = = "") return $utfstr;
if (Empty ($UC 2GBTABLE)) {
$filename = DirName (__file__). " /data/gb2312-utf8.table ";
$fp = fopen ($filename, "R");
while ($l = fgets ($fp, 15))
{$UC 2gbtable[hexdec (substr ($l, 7, 6)] = Hexdec (substr ($l, 0, 6));}
Fclose ($FP);
}
$okstr = "";
$ulen = strlen ($UTFSTR);
for ($i =0; $i < $ulen; $i + +)
{
$c = $utfstr [$i];
$CB = Decbin (Ord ($utfstr [$i]));
if (strlen ($CB) ==8) {
$csize = Strpos (Decbin (Ord ($CB)), "0");
for ($j =0; $j < $csize; $j + +) {
$i + +; $c. = $utfstr [$i];
}
$c = utf82u ($c);
if (Isset ($UC 2gbtable[$c])) {
$c = Dechex ($UC 2gbtable[$c]+0x8080);
$okstr. = Chr (Hexdec ($c [0]. $c [1])). Chr (Hexdec ($c [2]. $c [3]));
}
Else
{$okstr. = "&#". $c. ";";}
}
else $okstr. = $c;
}
$okstr = Trim ($OKSTR);
return $okstr;
}