<?php
/**
拼音碼表的生成:
啟動輸入法生成器在逆轉換選項卡中裝入window的拼音輸入法並將其保存為文
本文件(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);
}
?>
應用例,給文字加上拼音<br>
<?
/**
應用例,給文字加上拼音。為簡化起見,假定全為中文
*/
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 = " 拼音碼表的生成:
啟動輸入法生成器在逆轉換選項卡中裝入window的拼音輸入法並將其保存為文
本文件(winpy.txt)備用。
下面的代碼將拼音碼表文件(文本)裝入到數組$pymb中,結構為:(拼音,(漢字...))
碼表轉換時間較長,應轉換後另行保存
";
$ar = get_py($text);
//print_r($ar);
echo "<table><tr align=center>";
for($i=0;$i<count($ar);$i++) {
echo "<td>".$ar[$i][1]."</td>";
}
echo "</tr><tr align=center>";
for($i=0;$i<count($ar);$i++) {
echo "<td>".$ar[$i][0]."</td>";
}
echo "</tr></table>";
?>