<? Php
/**
Generation of the pinyin code table:
Start the input method generator, load the window PinYin Input Method in the inverse Conversion tab, and save it as a text
This file (winpy.txt) is used up.
The following code loads the pinyin code table file (text) into the array $ pymb. The structure is: (pinyin, (Chinese character ...))
Code table conversion takes a long time, which should be converted and saved separately
*/
$ 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, "% 2 s % s", $ ch, $ py );
If ($ ch> = "ah" & ord ($ py) <128 ){
$ Pymb [$ ch] = $ py;
}
}
Fclose ($ fp );
$ Fp = fopen ("pymb.txt", "w ");
Fwrite ($ fp, serialize ($ pymb). "\ n ");
Fclose ($ fp );
}
?>
Application Example: Add pinyin to text <br>
<?
/**
Application Example: Add pinyin to the text. For simplicity, it is assumed that all are Chinese
*/
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 = "pinyin code table generation:
Start the input method generator, load the window PinYin Input Method in the inverse Conversion tab, and save it as a text
This file (winpy.txt) is used up.
The following code loads the pinyin code table file (text) into the array $ pymb. The structure is: (pinyin, (Chinese character ...))
Code table conversion takes a long time, which should be converted and saved separately
";
$ 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> ";
?>