您当前的位置: 首页 > 网站编程 > PHP教程 > php汉字转拼音的示例

php汉字转拼音的示例

作者:不详 来源:网络 发布时间: 2014-08-05 21:31 点击:
这篇文章主要介绍了php汉字转拼音的示例,需要的朋友可以参考下 代码如下: ?php class Helper_Spell{ public $spellArray = array(); static public function getArray() { return unserialize(file_get_contents('pytable_without_tune.txt')); } /** * @desc 获取字符

php汉字转拼音的示例

  这篇文章主要介绍了php汉字转拼音的示例,需要的朋友可以参考下

  代码如下:

  <?php

  class Helper_Spell{

  public $spellArray = array();

  static public function getArray() {

  return unserialize(file_get_contents('pytable_without_tune.txt'));

  }

  /**

  * @desc 获取字符串的首字母

  * @param $string 要转换的字符串

  * @param $isOne 是否取首字母

  * @param $upper 是否转换为大写

  * @return string

  *

  * 例如:getChineseFirstChar('我是作者') 首字符全部字母+小写

  * return "wo"

  *

  * 例如:getChineseFirstChar('我是作者',true) 首字符首字母+小写

  * return "w"

  *

  * 例如:getChineseFirstChar('我是作者',true,true) 首字符首字母+大写

  * return "W"

  *

  * 例如:getChineseFirstChar('我是作者',false,true) 首字符全部字母+大写

  * return "WO"

  */

  static public function getChineseFirstChar($string,$isOne=false,$upper=false) {

  $spellArray = self::getArray();

  $str_arr = self::utf8_str_split($string,1); //将字符串拆分成数组

  if(preg_match('/^[x{4e00}-x{9fa5}]+$/u',$str_arr[0])) { //判断是否是汉字

  $chinese = $spellArray[$str_arr[0]];

  $result = $chinese[0];

  }else {

  $result = $str_arr[0];

  }

  $result = $isOne ? substr($result,0,1) : $result;

  return $upper?strtoupper($result):$result;

  }

  /**

  * @desc 将字符串转换成拼音字符串

  * @param $string 汉字字符串

  * @param $upper 是否大写

  * @return string

  *

  * 例如:getChineseChar('我是作者'); 全部字符串+小写

  * return "wo shi zuo zhe"

  *

  * 例如:getChineseChar('我是作者',true); 首字母+小写

  * return "w s z z"

  *

  * 例如:getChineseChar('我是作者',true,true); 首字母+大写

  * return "W S Z Z"

  *

  * 例如:getChineseChar('我是作者',false,true); 首字母+大写

  * return "WO SHI ZUO ZHE"

  */

  static public function getChineseChar($string,$isOne=false,$upper=false) {

  global $spellArray;

  $str_arr = self::utf8_str_split($string,1); //将字符串拆分成数组

  $result = array();

  foreach($str_arr as $char)

  {

  if(preg_match('/^[x{4e00}-x{9fa5}]+$/u',$char))

  {

  $chinese = $spellArray[$char];

  $chinese = $chinese[0];

  }else{

  $chinese=$char;

  }

  $chinese = $isOne ? substr($chinese,0,1) : $chinese;

  $result[] = $upper ? strtoupper($chinese) : $chinese;

  }

  return implode(' ',$result);

  }

  /**

  * @desc 将字符串转换成数组

  * @param $str 要转换的数组

  * @param $split_len

  * @return array

  */

  private function utf8_str_split($str,$split_len=1) {

  if(!preg_match('/^[0-9]+$/', $split_len) || $split_len < 1) {

  return FALSE;

  }

  $len = mb_strlen($str, 'UTF-8');

  if ($len <= $split_len) {

  return array($str);

  }

  preg_match_all('/.{'.$split_len.'}|[^x00]{1,'.$split_len.'}$/us', $str, $ar);

  return $ar[0];

  }

  }
分享到:
本文"php汉字转拼音的示例"由远航站长收集整理而来,仅供大家学习与参考使用。更多网站制作教程尽在远航站长站。
顶一下
(0)
0%
踩一下
(0)
0%
[点击 次] [返回上一页] [打印]
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 密码: 验证码:
关于本站 - 联系我们 - 网站声明 - 友情连接- 网站地图 - 站点地图 - 返回顶部
Copyright © 2007-2013 www.yhzhan.com(远航站长). All Rights Reserved .
远航站长:为中小站长提供最佳的学习与交流平台,提供网页制作与网站编程等各类网站制作教程.
官方QQ:445490277 网站群:26680406 网站备案号:豫ICP备07500620号-4