PHP常用函数之获取汉字首字母功能示例

本文实例讲述了PHP常用函数之获取汉字首字母功能。分享给大家供大家参考,具体如下:

//获取汉字的首字母
function getFirstCharters($str)
{
  if (empty($str)) {
    return '';
  }
  //取出参数字符串中的首个字符
  $temp_str = substr($str,1);
  if(ord($temp_str) > 127){
    $str = substr($str,3);
  }else{
    $str = $temp_str;
    $fchar = ord($str{0});
    if ($fchar >= ord('A') && $fchar <= ord('z')){
      return strtoupper($temp_str);
    }else{
      return null;
    }
  }
  $s1 = iconv('UTF-8','gb2312//IGNORE',$str);
  if(empty($s1)){
    return null;
  }
  $s2 = iconv('gb2312','UTF-8',$s1);
  if(empty($s2)){
    return null;
  }
  $s = $s2 == $str ? $s1 : $str;
  $asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
  if ($asc >= -20319 && $asc <= -20284)
    return 'A';
  if ($asc >= -20283 && $asc <= -19776)
    return 'B';
  if ($asc >= -19775 && $asc <= -19219)
    return 'C';
  if ($asc >= -19218 && $asc <= -18711)
    return 'D';
  if ($asc >= -18710 && $asc <= -18527)
    return 'E';
  if ($asc >= -18526 && $asc <= -18240)
    return 'F';
  if ($asc >= -18239 && $asc <= -17923)
    return 'G';
  if ($asc >= -17922 && $asc <= -17418)
    return 'H';
  if ($asc >= -17417 && $asc <= -16475)
    return 'J';
  if ($asc >= -16474 && $asc <= -16213)
    return 'K';
  if ($asc >= -16212 && $asc <= -15641)
    return 'L';
  if ($asc >= -15640 && $asc <= -15166)
    return 'M';
  if ($asc >= -15165 && $asc <= -14923)
    return 'N';
  if ($asc >= -14922 && $asc <= -14915)
    return 'O';
  if ($asc >= -14914 && $asc <= -14631)
    return 'P';
  if ($asc >= -14630 && $asc <= -14150)
    return 'Q';
  if ($asc >= -14149 && $asc <= -14091)
    return 'R';
  if ($asc >= -14090 && $asc <= -13319)
    return 'S';
  if ($asc >= -13318 && $asc <= -12839)
    return 'T';
  if ($asc >= -12838 && $asc <= -12557)
    return 'W';
  if ($asc >= -12556 && $asc <= -11848)
    return 'X';
  if ($asc >= -11847 && $asc <= -11056)
    return 'Y';
  if ($asc >= -11055 && $asc <= -10247)
    return 'Z';
  return rare_words($asc);
}
//百家姓中的生僻字
function rare_words($asc=''){
  $rare_arr = array(
    -3652=>array('word'=>"窦",'first_char'=>'D'),-8503=>array('word'=>"奚",'first_char'=>'X'),-9286=>array('word'=>"酆",'first_char'=>'F'),-7761=>array('word'=>"岑",'first_char'=>'C'),-5128=>array('word'=>"滕",'first_char'=>'T'),-9479=>array('word'=>"邬",'first_char'=>'W'),-5456=>array('word'=>"臧",'first_char'=>'Z'),-7223=>array('word'=>"闵",'first_char'=>'M'),-2877=>array('word'=>"裘",'first_char'=>'Q'),-6191=>array('word'=>"缪",-5414=>array('word'=>"贲",'first_char'=>'B'),-4102=>array('word'=>"嵇",'first_char'=>'J'),-8969=>array('word'=>"荀",-4938=>array('word'=>"於",'first_char'=>'Y'),-9017=>array('word'=>"芮",'first_char'=>'R'),-2848=>array('word'=>"羿",-9477=>array('word'=>"邴",-9485=>array('word'=>"隗",'first_char'=>'K'),-6731=>array('word'=>"宓",-9299=>array('word'=>"郗",-5905=>array('word'=>"栾",'first_char'=>'L'),-4393=>array('word'=>"钭",-9300=>array('word'=>"郜",'first_char'=>'G'),-8706=>array('word'=>"蔺",-3613=>array('word'=>"胥",-8777=>array('word'=>"莘",'first_char'=>'S'),-6708=>array('word'=>"逄",'first_char'=>'P'),-9302=>array('word'=>"郦",-5965=>array('word'=>"璩",-6745=>array('word'=>"濮",-4888=>array('word'=>"扈",'first_char'=>'H'),-9309=>array('word'=>"郏",-5428=>array('word'=>"晏",-2849=>array('word'=>"暨",-7206=>array('word'=>"阙",-4945=>array('word'=>"殳",-9753=>array('word'=>"夔",-10041=>array('word'=>"厍",-5429=>array('word'=>"晁",-2396=>array('word'=>"訾",-7205=>array('word'=>"阚",-10049=>array('word'=>"乜",'first_char'=>'N'),-10015=>array('word'=>"蒯",-3133=>array('word'=>"竺",-6698=>array('word'=>"逯",-9799=>array('word'=>"俟",-6749=>array('word'=>"澹",-7220=>array('word'=>"闾",-10047=>array('word'=>"亓",-10005=>array('word'=>"仉",-3417=>array('word'=>"颛",-6431=>array('word'=>"驷",-7226=>array('word'=>"闫",-9293=>array('word'=>"鄢",-6205=>array('word'=>"缑",-9764=>array('word'=>"佘",-9818=>array('word'=>"佴",-9509=>array('word'=>"谯",-3122=>array('word'=>"笪",-9823=>array('word'=>"佟",);
  if(array_key_exists($asc,$rare_arr) && $rare_arr[$asc]['first_char']){
    return $rare_arr[$asc]['first_char'] ;
  }else{
    return null;
  }
}
//测试:
echo getFirstCharters('窦');

运行结果:

D

PS:这里再为大家提供几款本站拼音与字母相关工具供大家参考:

在线中英文根据首字母排序工具:
http://tools.jb51.net/aideddesign/zh_paixu

在线汉字转换成拼音工具:
http://tools.jb51.net/transcoding/pinyin

在线中文汉字转拼音工具:
http://tools.jb51.net/transcoding/hanzi2pinyin

在线中文汉字拼音对照转换工具:
http://tools.jb51.net/transcoding/zh_pinyin

在线字母大小写转换工具:
http://tools.jb51.net/transcoding/upper

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP编码与转码操作技巧汇总》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《php常用函数与技巧总结》及《PHP错误与异常处理方法总结》

希望本文所述对大家PHP程序设计有所帮助。

相关文章

文章浏览阅读8.4k次,点赞8次,收藏7次。SourceCodester Onl...
文章浏览阅读3.4k次,点赞46次,收藏51次。本文为大家介绍在...
文章浏览阅读1.1k次。- php是最优秀, 最原生的模板语言, 替代...
文章浏览阅读1.1k次,点赞18次,收藏15次。整理K8s网络相关笔...
文章浏览阅读1.2k次,点赞22次,收藏19次。此网络模型提供了...
文章浏览阅读1.1k次,点赞14次,收藏19次。当我们谈论网络安...