加入收藏 | 设为首页 | 会员中心 | 我要投稿 阜新站长网 (https://www.0418zz.cn/)- 基础存储、数据处理、视频终端、内容创作、网络安全!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php把汉字转换成拼音代码

发布时间:2022-06-17 09:15:01 所属栏目:PHP教程 来源:互联网
导读:下面有三个函数对应的是取汉字码,与转换成相对就的拼音,我们的实例是简单的,只举了a开头的汉字转换拼音的实例代码. $piny = array( a=-20319, ai=-20317, an=-20304, ang=-20295 ); echo getChineseSpells(中国WEB第一站 www.phpfensi.com); //取汉字所有拼
  下面有三个函数对应的是取汉字码,与转换成相对就的拼音,我们的实例是简单的,只举了a开头的汉字转换拼音的实例代码.
 
  $piny = array(
      'a'=>-20319,
      'ai'=>-20317,
      'an'=>-20304,
      'ang'=>-20295
      );
  echo  getChineseSpells('中国WEB第一站 www.phpfensi.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;
   }
 

(编辑:阜新站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读