<? Php
$ Pinyins = Array ();
// When the ishead of a Chinese character is 0, the first letter of the Chinese character is output when the ishead of all Chinese characters is 1.
Function GetPinyin ($ str, $ ishead = 0, $ isclose = 1 ){
Global $ pinyins;
$ Restr = "";
$ Str = trim ($ str );
$ Slen = strlen ($ str );
If ($ slen <2) return $ str;
If (count ($ pinyins) = 0 ){
$ Fp = fopen ("./pinyin. db", "r ");
While (! Feof ($ fp )){
$ Line = trim (fgets ($ fp ));
$ Pinyins [$ line [0]. $ line [1] = substr ($ line, 3, strlen ($ line)-3 );
}
Fclose ($ fp );
}
For ($ I = 0; $ I <$ slen; $ I ++ ){
If (ord ($ str [$ I])> 0x80)
{
$ C = $ str [$ I]. $ str [$ I + 1];
$ I ++;
If (isset ($ pinyins [$ c]) {
If ($ ishead = 0) $ restr. = $ pinyins [$ c];
Else $ restr. = $ pinyins [$ c] [0];
} Else $ restr. = "_";
} Else if (eregi ("[a-z0-9]", $ str [$ I]) {$ restr. = $ str [$ I];}
Else {$ restr. = "_";}
}
If ($ isclose = 0) unset ($ pinyins );
Return $ restr;
}
If ($ _ GET ['hanzi'])
{
Echo $ pinyin = GetPinyin ($ _ GET ['hanzi'], 1, 0 );
// Echo ":". $ pinyin;
}
?>