汉字转拼音---PHP

思路:

在文件中放入一个UTF-8的中文汉字与拼音对照的文件。在汉字转拼音时,只要用正则匹配到对照文件的相应的汉字,就能得到拼音。

汉字与拼音对照文件下载地址:链接:https://pan.baidu.com/s/1kIgGmP7mAKnRbchetqXrwQ 密码:6ezn

汉字转拼音类:

<?php
/**
*
* 汉字转拼音类
* @Author : Kin
* @Date   : 2014-03-16
* @Email  : Mr.kin@foxmail.com
*
*/

class ChinesePinyin{
    
    //utf-8中国汉字集合
    private $ChineseCharacters;
    //编码
    private $charset = 'utf-8';
    
    public function __construct(){
        if( empty($this->ChineseCharacters) ){
         $this->ChineseCharacters = file_get_contents('ChineseCharacters.dat','r');
        }
    }
    
    /*
    * 转成带有声调的汉语拼音
    * param $input_char String  需要转换的汉字
    * param $delimiter  String   转换之后拼音之间分隔符
    * param $outside_ignore  Boolean     是否忽略非汉字内容
    */
    public function TransformWithTone($input_char,$delimiter='',$outside_ignore=false){
        
        $input_len = mb_strlen($input_char,$this->charset);
        $output_char = '';
        for($i=0;$i<$input_len;$i++){
            $word = mb_substr($input_char,$i,1,$this->charset);
            if(preg_match('/^[\x{4e00}-\x{9fa5}]$/u',$word) && preg_match('/\,'.preg_quote($word).'(.*?)\,/',$this->ChineseCharacters,$matches) ){
                $output_char.=$matches[1].$delimiter;
            }else if(!$outside_ignore){
                $output_char.=$word;
            }
        }
        
        return $output_char;
    }
    
    /*
    * 转成带无声调的汉语拼音
    * param $input_char String  需要转换的汉字
    * param $delimiter  String   转换之后拼音之间分隔符
    * param $outside_ignore  Boolean     是否忽略非汉字内容
    */    
    public function TransformWithoutTone($input_char,$delimiter='',$outside_ignore=true){
        
        $char_with_tone = $this->TransformWithTone($input_char,$delimiter,$outside_ignore);
        
        $char_without_tone  =  str_replace(array('ā','á','ǎ','à','ō','ó','ǒ','ò','ē','é','ě','è','ī','í','ǐ','ì','ū','ú','ǔ','ù','ǖ','ǘ','ǚ','ǜ','ü'),
                                           array('a','a','a','a','o','o','o','o','e','e','e','e','i','i','i','i','u','u','u','u','v','v','v','v','v')
                                           ,$char_with_tone );
        return $char_without_tone;
        
    }
    
    /*
    * 转成汉语拼音首字母
    * param $input_char String  需要转换的汉字
    * param $delimiter  String   转换之后拼音之间分隔符
    */    
    public function TransformUcwords($input_char,$delimiter=''){
        
        $char_without_tone = ucwords($this->TransformWithoutTone($input_char,' ',true));
        $ucwords = preg_replace('/[^A-Z]/','',$char_without_tone);
        if(!empty($delimiter)){
            $ucwords = implode($delimiter,str_split($ucwords));
        }
        return strtolower($ucwords);        
    }
    
}

 

转载于:https://www.cnblogs.com/Mr-Wenyan/p/9039946.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值