/**
拼音碼錶的產生:
啟動IME產生器在逆轉換選項卡中裝入window的拼音IME並將其儲存為文
本檔案(winpy.txt)備用。
下面的代碼將拼音碼錶檔案(文本)裝入到數組$pymb中,結構為:(拼音,(漢字...))
碼錶轉換時間較長,應轉換後另行儲存
*/
$filename = "pymb.txt";
if(file_exists($filename)) {
$fp = fopen($filename,"r");
$pymb = unserialize(fread($fp,filesize($filename)));
fclose($fp);
}else {
$filename = "winpy.txt";
$fp = fopen($filename,"r");
$old = "";
$ar = array();
$pymb = array();
while(! feof($fp)) {
$buffer = fgets($fp,128);
sscanf($buffer,"%2s%s",$ch,$py);
if($ch >= "啊" && ord($py) < 128) {
$pymb[$ch] = $py;
}
}
fclose($fp);
$fp = fopen("pymb.txt","w");
fwrite($fp,serialize($pymb)."\n");
fclose($fp);
}
?>
應用例,給文字加上拼音
/**
應用例,給文字加上拼音。為簡化起見,假定全為中文
*/
function get_py($text) {
global $pymb;
$i = 0;
$n = strlen($text);
$ar = array();
while($i<$n) {
$ch = $text[$i++];
$py = " ";
if(ord($ch) > 128) {
$ch .= $text[$i++];
$py = $pymb[$ch];
}
$ar[] = array($ch,$py);
}
return $ar;
}
$text = " 拼音碼錶的產生:
啟動IME產生器在逆轉換選項卡中裝入window的拼音IME並將其儲存為文
本檔案(winpy.txt)備用。
下面的代碼將拼音碼錶檔案(文本)裝入到數組$pymb中,結構為:(拼音,(漢字...))
碼錶轉換時間較長,應轉換後另行儲存
";
$ar = get_py($text);
//PRint_r($ar);
echo "
";for($i=0;$i echo "
".$ar[$i][1]." | ";}echo "
";for($i=0;$i echo "
".$ar[$i][0]." | ";}echo "
";
?>