分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

php汉字转拼音

发布时间:2023-09-06 01:22责任编辑:郭大石关键词:暂无标签

完整下载:

http://dwz.cn/6NnaP0

核心代码如下:

<?php/**** 汉字转拼音类**/class ChinesePinyin{//utf-8中国汉字集合private $ChineseCharacters;//编码private $charset = ‘utf-8‘;public function __construct(){if( empty($this->ChineseCharacters) ){ ?$this->ChineseCharacters = file_get_contents(‘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 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 $ucwords;}}?>

使用示例:

<?phpinclude ‘ChinesePinyin.class.php‘;$Pinyin = new ChinesePinyin();$words = ‘汉字转成拼音类‘;echo ‘<h2>‘.$words.‘</h2>‘;echo ‘<p>转成带有声调的汉语拼音<br/>‘;$result = $Pinyin->TransformWithTone($words);echo $result,‘</p>‘;echo ‘<p>转成带无声调的汉语拼音<br/>‘;$result = $Pinyin->TransformWithoutTone($words,‘ ‘);echo($result),‘</p>‘;echo ‘<p>转成汉语拼音首字母<br/>‘;$result = $Pinyin->TransformUcwords($words);echo($result),‘</p>‘;

  

;

php汉字转拼音

原文地址:http://www.cnblogs.com/upup2015/p/7786811.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved