Generation of Pinyin code table

Source: Internet
Author: User

<? 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> ";
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.