php汉字转拼音_PHP教程

WBOY
Lepaskan: 2016-07-13 10:26:43
asal
1073 orang telah melayarinya

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>
?>
Salin selepas log masuk

包含的字典见附件: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...
Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan