栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP > PHP实例

PHP汉字转拼音函数类示例用法

PHP实例 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

PHP汉字转拼音函数类示例用法

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


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

代码:

ChineseCharacters) ){
		  $this->ChineseCharacters = file_get_contents(PINYIN_ROOT.'/Pinyin/ChineseCharacters.dat');
		}
	}

	
	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;
	}

	
	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;

	}

	
	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;


	}


	
	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文件

代码:

TransformWithTone($str);
$pinyin2 = $Pinyin->TransformWithoutTone($str);
$pinyin3 = $Pinyin->TransformUcwordsOnlyChar($str);
$pinyin4 = $Pinyin->TransformUcwords($str);
echo '带声调的汉语拼音: '.$pinyin1.'';
echo '
'; echo '无声调的汉语拼音: '.$pinyin2.''; echo '
'; echo '首字母只包括汉字: '.$pinyin3.''; echo '
'; echo '首字母和其他字符: '.$pinyin4.''; echo '
'; ?>


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


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

示例:http://liqingbo.cn/tools/pinyin/


源码下载地址:http://pan.baidu.com/s/1qWxJLQs 密码: r861

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/824896.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号