汉字转拼音

/*--------------------------
     * 汉字转拼音
     * $str     要转换的字符串
     * $split   是否要分割显示
     * $isfirst 是否返回首字母
     * $zwbd    是否过滤中文标点符号
     * http://files.cnblogs.com/files/helin/tools.zip
    -*------------------------*/
    function CHToPinyin($str,$split=true,$isfirst = false,$zwbd=true){ 
        $py = '';
        $str = trim($str);
        $bd_filter = array(',','。','?','!',':','“','”',';','‘','’','《','》',
                     '{','}','【','】','(',')','—','〈','〉','〔','〕','﹄','﹃','﹂','﹁','~');
        $str_arr = preg_split('/(?<!^)(?!$)/u', $str );
        foreach($str_arr as $code){
            if( in_array($code,$bd_filter) ){
                if($zwbd){
                    $py .= $code;
                }    
            }elseif( strlen($code) == 3 ){
                //常用汉字
                static $_cnfonts_min;
                if(!isset($_cnfonts_min)){
                    include_once '_cnfonts_min.php';//$_cnfonts_min='...';
                }
                if(isset($_cnfonts_min[$code])){
                    $py .= ( $split && $py ? ' ' : '' ).($isfirst ? strtoupper($_cnfonts_min[$code][0]) : $_cnfonts_min[$code]);
                }else{
                    //扩展汉字
                    static $_cnfonts_ext;
                    if(!isset($_cnfonts_ext)){
                        include_once '_cnfonts_ext.php';//$_cnfonts_ext='...';
                    }
                    if(isset($_cnfonts_ext[$code])){
                        $py .= ( $split && $py ? ' ' : '' ).($isfirst ? strtoupper($_cnfonts_ext[$code][0]) : $_cnfonts_ext[$code]);
                    }else{
                        $py .= $code;
                    }
                }
            }else{
                $py .= $code;
            }
        }
        return $py;
    } 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值