<? 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 Code below loads the pinyin code table file (text) into an array $ pymb with the structure: (pinyin, (Chinese characters ...))
the conversion time of a code table is long. It should be converted and stored 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;
}< BR >}< br> fclose ($ FP);
$ fp = fopen ("pymb.txt", "W ");
fwrite ($ FP, serialize ($ pymb ). "\ n");
fclose ($ FP);
}< BR >?>
application example: Add pinyin to text
/**
example: Add pinyin to the text. For the sake of simplification, 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> ";
?>