& Lt ;? Php ** Pinyin code table generation: the startup input method generator is installed with the windowless pinyin input method in the reverse conversion card and saved as the audio file (winpy.txt) for backup. The following code loads the Pinyin code table File (text) into the array $ pymb. The structure is: (pinyin, (Chinese character ...)) the conversion time of the code table is long. convert the data table and save the * $ filename & quot; pymb. t /**
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
/**
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"
";For ($ I = 0; $ IEcho"
". $ Ar [$ I] [1]." | ";}Echo"
";For ($ I = 0; $ IEcho"
". $ Ar [$ I] [0]." | ";}Echo"
";
?>