php汉字转拼音_PHP教程

WBOY
Freigeben: 2016-07-13 10:26:43
Original
1074 Leute haben es durchsucht

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>
?>
Nach dem Login kopieren

包含的字典见附件: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...
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage