Home > Backend Development > PHP Tutorial > Convert Chinese characters to Pinyin in php_PHP tutorial

Convert Chinese characters to Pinyin in php_PHP tutorial

WBOY
Release: 2016-07-13 10:26:43
Original
1096 people have browsed it

php Chinese characters to pinyin

php function (modified by the SpGetPinyin function of dedecms (dedecms/include/inc/inc_fun_funAdmin.php), the dictionary of dedecms is not complete):

<?<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>
?>
Copy after login

See the attachment for the included dictionary: pinyin.rar/pinyin.dic (gbk)

Download address: http://files.cnblogs.com/luoyunshu/pinyin.rar

Reference:

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/822275.htmlTechArticlephp Chinese character to pinyin php function (modified by the SpGetPinyin function of dedecms (dedecms/include/inc/inc_fun_funAdmin.php) , the dictionary of dedecms is not complete): ? php function pinyin( $str , $is...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template