Use of coded tables (2)

Source: Internet
Author: User
Tags array comparison comparison table key sql mysql pack
Coding <?php
/**
* This procedure is used to load the simplified code from the coding table and to shorten the loading time by serialization
* also provides two simplified conversion functions
* Use method:
* Add in the required program
* require_once "load_gb_big5.php";
* SetChar (["Big5"]);
*/

$filename = "Gb_big5.txt";
if (!file_exists ($filename)) {
/**
* Connect and open the comparison table
*/
$conn = mysql_connect ();
mysql_select_db ("Unicode");
/**
* Extract Big5 and GBK encoded in the table with only 13,487 Big5 (including symbols)
*/
$sql = "Select gbk,big5,gb2312 from Unicode where Big5!= '";
$rs = mysql_query ($sql);
/**
* Read the data to the array, pay attention to the key and value processing
*/
$code = Array (0=> "Big5", gb2312=>0);

while ($row = Mysql_fetch_array ($rs)) {
$code [Pack ("H4", $row][GBK])] = Pack ("H4", $row [Big5]);
}
Mysql_close ($conn);
$fp = fopen ($filename, "w");
Fwrite ($FP, serialize ($code));
Fclose ($FP);
}else {
$fp = fopen ($filename, "R");
$code = Unserialize (Fread ($fp, FileSize ($filename));
Fclose ($FP);
}

/**
* Convert input string to corresponding encoding
*/
function GetChar ($s) {
Global $code;
$v = "";
for ($i =0; $i <strlen ($s); $i + +) {
if (Ord ($s [$i]) > 0x7f) {
$p = sprintf ("%02x%02x", $s [$i], $s [$i +1]);
$p = $s [$i]. $s [$i +1];
$ch = $code [$p];
if ($ch = = "")
$ch = $s [$i]. $s [$i +1];
$v. = $ch;
$i + +;
}else
$v. = $s [$i];
}
return $v;
}

/**
* Set conversion type and lose out
* There is no other output before this function
*/
function SetChar ($char = "gb2312") {
Global $code;
if ($char!= "gb2312" && $char!= "Big5")
Die ("character encoding type wrong!!!");
if ($code [0]!= $char)
$code = Array_flip ($code); Exchange key if not the specified encoding type
Header ("content-type:text/html; charset= $char ");
}

?>
Test Example:
<?php
Require_once "load_gb_big5.php";
SetChar ("Big5");

Echo GetChar ('
Recently completed the GBK, gb2312, Big5 coding comparison table (including its Hanyu Pinyin) collation
Now in a text file to provide to you, compression package 297k
Where: (all include symbols)
GBK 21791
Big5 15863
gb2312 7478
Note that the data row "," must be under MySQL, or it may be wrong. Some friends get the new version may not, please add.
(2002-10-24)
Interested friends please contact me, email:czjsz_ah@stats.gov.cn

Document Format:
"B6f6", "" "," B6f6 "," 997F "," Hungry "," E ",
"B6f7", "AEA6", "B6f7", "6069", "en", "en",
"B6f8", "a6d3", "B6f8", "800C", "and", "Er|neng",
"B6f9", "A449", "b6f9", "513F", "Son", "Er|ren",
Order: GBK, Big5, gb2312, Unicode, font, pinyin

MySQL Build table code (please change according to the actual situation):
');
?>


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.