首頁 > 後端開發 > php教程 > php汉字转拼音_PHP教程

php汉字转拼音_PHP教程

WBOY
發布: 2016-07-13 10:26:43
原創
1087 人瀏覽過

php汉字转拼音

php函数(由dedecms(dedecms/include/inc/inc_fun_funAdmin.php)的SpGetPinyin函数修改,dedecms的字典不太完全):

<?<span>php

    </span><span>function</span> pinyin(<span>$str</span>, <span>$ishead</span><span>) {
        </span><span>static</span> <span>$pinyins</span> = <span>array</span><span>();
    
        </span><span>$restr</span> = ''<span>;
        </span><span>$str</span> = <span>trim</span>(<span>$str</span><span>);
        </span><span>$slen</span> = <span>strlen</span>(<span>$str</span><span>);
        </span><span>if</span>(<span>$slen</span> < 2<span>) {
            </span><span>return</span> <span>$str</span><span>;
        }

        </span><span>if</span>(<span>count</span>(<span>$pinyins</span>) == 0<span>) {
            </span><span>$fp</span> = <span>fopen</span>('pinyin.dic', 'r'<span>);
            </span><span>while</span>(!<span>feof</span>(<span>$fp</span><span>)) {
                </span><span>$line</span> = <span>trim</span>(<span>fgets</span>(<span>$fp</span><span>));
                </span><span>$a2</span> = <span>explode</span>('`', <span>$line</span><span>);
                </span><span>isset</span>(<span>$a2</span>[1]) && <span>$pinyins</span>[<span>$a2</span>[0]] = <span>$a2</span>[1<span>];
            }
            </span><span>fclose</span>(<span>$fp</span><span>);
        }
        
        </span><span>for</span>(<span>$i</span>=0; <span>$i</span><<span>$slen</span>; <span>$i</span>++<span>) {
            </span><span>if</span>(<span>ord</span>(<span>$str</span>[<span>$i</span>])>0x80<span>) {
                </span><span>$c</span> = <span>$str</span>[<span>$i</span>].<span>$str</span>[<span>$i</span>+1<span>];
                </span><span>$i</span>++<span>;
                </span><span>if</span>(<span>isset</span>(<span>$pinyins</span>[<span>$c</span><span>])) {
                    </span><span>$restr</span>.= (<span>$ishead</span>==0)?<span>$pinyins</span>[<span>$c</span>]:<span>$pinyins</span>[<span>$c</span>][0<span>];
                }</span><span>else</span><span> {
                    </span><span>$restr</span> .= "_"<span>;
                }
            }</span><span>else</span> <span>if</span>( <span>preg_match</span>("/[a-z0-9]/i", <span>$str</span>[<span>$i</span><span>]) ) {
                </span><span>$restr</span> .= <span>$str</span>[<span>$i</span><span>];
            }
            </span><span>else</span><span> {
                </span><span>$restr</span> .= "_"<span>;
            }
        }

        </span><span>return</span> <span>$restr</span><span>;
    }
    
    </span><span>//</span><span>测试</span>
    <span>echo</span> pinyin('舒熱佳隔热膜',1),'<br>'<span>; 
    </span><span>echo</span> pinyin('舒熱佳隔热膜',0),'<br>'<span>; 
    </span><span>echo</span> pinyin('鹦鹉',1),'<br>'<span>;  
    </span><span>echo</span> pinyin('鹦鹉',0),'<br>'<span>;  
    </span><span>echo</span> pinyin('眠之堡/依诺维绅/myside床垫',1),'<br>'<span>; 
    </span><span>echo</span> pinyin('眠之堡/依诺维绅/myside床垫',0),'<br>'<span>;  
    
    </span><span>/*</span><span>结果:
    srjgrm
    shurejiageremo
    yw
    yingwu
    mzb_ynws_mysidecd
    mianzhibao_yinuoweishen_mysidechuangdian</span><span>*/</span>
?>
登入後複製

包含的字典见附件:pinyin.rar/pinyin.dic (gbk)

下载地址:http://files.cnblogs.com/luoyunshu/pinyin.rar

参考:

http://www.oschina.net/code/snippet_862384_25415

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/822275.htmlTechArticlephp汉字转拼音 php函数(由dedecms(dedecms/include/inc/inc_fun_funAdmin.php)的SpGetPinyin函数修改,dedecms的字典不太完全): ? php function pinyin( $str , $is...
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板