分享

PHP汉字转拼音函数类示例

 马超读书的季节 2015-12-25
在PHP中如何将中文汉字转换成对应的拼音或者首字母等功能呢,现在我们就来讲解一下PHP所提供的汉字转拼音类。


    首先要实现这功能需要包含一个ChinesePinyin.class.php类

代码:

<?php
/**
*
* 汉字转拼音类
* @Author : Kin
* @Date   : 2014-03-16
* @Email  : Mr.kin@foxmail.com
*
*/
namespace Org\Util;
define('PINYIN_ROOT'dirname(__FILE__));
class ChinesePinyin{
 
    //utf-8中国汉字集合
    private $ChineseCharacters;
    //编码
    private $charset = 'utf-8';
 
    public function __construct(){
        if( empty($this->ChineseCharacters) ){
          $this->ChineseCharacters = file_get_contents(PINYIN_ROOT.'/Pinyin/ChineseCharacters.dat');
        }
    }
 
    /*
    * 转成带有声调的汉语拼音
    * 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 TransformUcwordsOnlyChar($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 $ucwords;
 
 
    }
 
 
    /*
    * 转成汉语拼音首字母,包含非汉字内容
    * param $input_char String  需要转换的汉字
    * param $delimiter  String   转换之后拼音之间分隔符
    */
    public function TransformUcwords($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.= $delimiter.$word.$delimiter;
            }
        }
        $output_char  =  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')
                                           ,$output_char );
 
        $array = explode($delimiter,$output_char);
        $array = array_filter($array);
        $res = '';
        foreach($array as $list){
            $res .= substr($list,0,1);
        }
        return $res;
    }
 
 
 
 
}



我们来看一下如何调用这个类,并且操作这个类


以下是index.php文件

代码:

<?php
include 'ChinesePinyin.class.php';
$Pinyin = new \Org\Util\ChinesePinyin();
header("Content-Type:text/html;charset=utf-8");
$str = $_POST['str'];
if(strlen($str)<=0){
    echo '请输入要转换的内容';
    exit;
}
$pinyin1 = $Pinyin->TransformWithTone($str);
$pinyin2 = $Pinyin->TransformWithoutTone($str);
$pinyin3 = $Pinyin->TransformUcwordsOnlyChar($str);
$pinyin4 = $Pinyin->TransformUcwords($str);
echo '带声调的汉语拼音: <span class="red">'.$pinyin1.'</span>';
echo '<br>';
echo '无声调的汉语拼音: <span class="red">'.$pinyin2.'</span>';
echo '<br>';
echo '首字母只包括汉字: <span class="red">'.$pinyin3.'</span>';
echo '<br>';
echo '首字母和其他字符: <span class="red">'.$pinyin4.'</span>';
echo '<br>';
 
?>


把这两个文件放同一目录下,然后通过include函数包含,再通过实例化就能调用里面相应的方法了。


至于每个方法的功能ChinesePinyin.class.php类里面都有注释,自己可以认真看一下,我们也可以通过示例来看一下效果

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多