教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 把汉字转换成拼音代码

把汉字转换成拼音代码

发布时间:2016-10-27   编辑:jiaochengji.com
教程集为您提供把汉字转换成拼音代码等资源,欢迎您收藏本站,我们将为您提供最新的把汉字转换成拼音代码资源
上面有三个函数对应的是取汉字码,与转换成相对就的拼音,我们的实例是简单的,只举了a开头的汉字转换拼音的实例代码。

$piny = array(
    'a'=>-20319,
    'ai'=>-20317,
    'an'=>-20304,
    'ang'=>-20295
    );
echo  getChineseSpells('中国WEB第一站 www.jiaochengji.com');
//取汉字所有拼音
function getChineseSpells($chinese, $delimiter = ' ', $first=0)
 {
  $result = array();
  for ($i=0; $i<strlen($chinese); $i ) {
   $p = ord(substr($chinese,$i,1));
   if ($p>160) {
    $q = ord(substr($chinese, $i,1));
    $p = $p*256 $q - 65536;
   }
   $result[] = getChineseSpell($p);
   if ($first) {
    return $result[0];
   }
  }
  return implode($delimiter, $result);
 }
 
//取一个汉字码对应的拼音
function getChineseSpell ($num, $blank = '') {
  if ( $num>0 && $num<160 ) {
   return chr($num);
  } elseif ($num<-20319||$num>-10247) {
   return $blank;
  } else {
   foreach (chineseSpellList as $spell => $code) {
    if ($code > $num) break;
    $result = $spell;
   }
   return $result;
  }
 }

//功能,取汉字第一个拼音
function getFirstSpell($chinese, $length = 0) {
  $spell =getChineseSpells($chinese, ' ', 1);
  if ($length) {
   $spell = substr($spell, 0, $length);
  }
  return $spell;
 }
/*

*/

您可能感兴趣的文章:
js 汉字转拼音的代码
把汉字转换成拼音代码
PHP汉字拼音转换、公历农历转换的实例详解
vbs 汉字转拼音的函数(代码)
php实现汉字转拼音
php中怎么将中文转换拼音
java将中文汉字转成拼音的程序代码
php获取汉字中首字母(gb2312编码)的实现代码
php汉字转拼音的示例
Python 中拼音库 PyPinyin 的用法

[关闭]
~ ~