Php Chinese character to PinYin
Php functions (modified by the SpGetPinyin function of dedecms (dedecms/include/inc/inc_fun_funAdmin.php). The Dictionary of dedecms is incomplete ):
<? Php function pinyin ($ str, $ ishead) {static $ pinyins = array (); $ restr = ''; $ str = trim ($ str ); $ slen = strlen ($ str); if ($ slen <2) {return $ str;} if (count ($ pinyins) = 0) {$ fp = fopen ('pinyin. dic', 'R'); while (! Feof ($ fp) {$ line = trim (fgets ($ fp); $ a2 = explode (''', $ line); isset ($ a2 [1]) & $ pinyins [$ a2 [0] = $ a2 [1];} fclose ($ fp);} for ($ I = 0; $ I <$ slen; $ I ++) {if (ord ($ str [$ I])> 0x80) {$ c = $ str [$ I]. $ str [$ I + 1]; $ I ++; if (isset ($ pinyins [$ c]) {$ restr. = ($ ishead = 0 )? $ Pinyins [$ c]: $ pinyins [$ c] [0];} else {$ restr. = "_" ;}} else if (preg_match ("/[a-z0-9]/I", $ str [$ I]) {$ restr. = $ str [$ I];} else {$ restr. = "_" ;}}return $ restr;} // test echo pinyin ('su insulation film ', 1),' <br> '; echo pinyin ('shu Xiaojia thermal insulation film ', 0),' <br> '; echo pinyin ('parrot', 1), '<br> '; echo pinyin ('parrot ', 0),' <br> '; echo pinyin ('sleeping Castle/inoviswon/myside mattress', 1), '<br> '; echo pinyin ('sleeping Castle/inoviswon/myside fusion', 0), '<br>';/* result: srjgrm shureji Ageremo yw yingwu mzb_ynws_mysidecd mianzhibao_yinuoweishen_mysidechuangdian */?>
See the attachment for the contained dictionaries: pinyin.rar/pinyin. dic (gbk)
: Http://files.cnblogs.com/luoyunshu/pinyin.rar
Refer:
Http://www.oschina.net/code/snippet_862384_25415