>백엔드 개발 >PHP 튜토리얼 >php汉字转拼音_PHP教程

php汉字转拼音_PHP教程

WBOY
WBOY원래의
2016-07-13 10:26:431096검색

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...
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.