? Php/** Pinyin code table generation: the startup input method generator is installed with the windows' 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 relatively long. it should be converted and stored separately */$ filenamewinpy. /**
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 = "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 ){
If ($ py! = $ Old ){
If ($ old! = ""){
$ Pymb [] = array (py => $ old, hz => $ ar );
}
$ Old = $ py;
$ Ar = array ();
}
$ Ar [] = $ ch;
}
}
Fclose ($ fp );
?>
Application example: add pinyin to text
/**
Application example: add pinyin to the text. For simplicity, it is assumed that all are Chinese
*/
$ Text = "example: add pinyin to text ";
Echo"
";For ($ I = 0; $ I$ Ch = substr ($ text, $ I, 2 );$ K = "";For ($ p = 0; $ pIf (in_array ($ ch, $ pymb [$ p] [hz]) {$ K = $ pymb [$ p] [py];Break;}}Echo"
| $ K | ";}Echo"
";For ($ I = 0; $ I$ Ch = substr ($ text, $ I, 2 );Echo"
| $ Ch | ";}Echo"
";
?>