給文字加上拼音
發(fā)表時(shí)間:2023-07-31 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]<?php/**拼音碼表的生成:?jiǎn)?dòng)輸入法生成器在逆轉(zhuǎn)換選項(xiàng)卡中裝入window的拼音輸入法并將其保存為文本文件(winpy.txt)備用。下面的代碼將拼音碼表文件(文本)裝入到數(shù)組pymb中...
<?php
/**
拼音碼表的生成:
啟動(dòng)輸入法生成器在逆轉(zhuǎn)換選項(xiàng)卡中裝入window的拼音輸入法并將其保存為文
本文件(winpy.txt)備用。
下面的代碼將拼音碼表文件(文本)裝入到數(shù)組$pymb中,結(jié)構(gòu)為:(拼音,(漢字...))
碼表轉(zhuǎn)換時(shí)間較長(zhǎng),應(yīng)轉(zhuǎn)換后另行保存
*/
$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) {
if($py != $old) {
if($old != "") {
$pymb[] = array(py => $old,hz => $ar);
}
$old = $py;
$ar = array();
}
$ar[] = $ch;
}
}
fclose($fp);
?>
應(yīng)用例,給文字加上拼音<br>
<?
/**
應(yīng)用例,給文字加上拼音。為簡(jiǎn)化起見,假定全為中文
*/
$text = "應(yīng)用例,給文字加上拼音";
echo "<table><tr align=center>";
for($i=0;$i<strlen($text);$i+=2) {
$ch = substr($text,$i,2);
$k = " ";
for($p=0;$p<count($pymb);$p++) {
if(in_array($ch,$pymb[$p][hz])) {
$k = $pymb[$p][py];
break;
}
}
echo "<td>$k</td>";
}
echo "</tr><tr align=center>";
for($i=0;$i<strlen($text);$i+=2) {
$ch = substr($text,$i,2);
echo "<td>$ch</td>";
}
echo "</tr>";
?>