国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php獲取中文字符拼音首字母實例

2024-05-04 21:58:08
字體:
來源:轉載
供稿:網友

在php中如果我們要獲取漢字的拼音第一個字母我們先要了解asc碼的范圍碼了,我們只要知道這個值上下限就可以使用php中的ord來獲取我相關的漢字拼音了.

實例1,代碼如下:

  1. function getFirstCharter($str) { 
  2.     if (emptyempty($str)) {return '';} 
  3.     $fchar = ord($str{0}); 
  4.     if ($fchar>=ord('A') && $fchar<=ord('z')) return strtoupper($str{0}); 
  5.     $s1 = iconv('UTF-8''gb2312'$str); 
  6.     $s2 = iconv('gb2312''UTF-8'$s1); 
  7.     $s = $s2 == $str ? $s1 : $str
  8.     $asc = ord($s{0})*256 + ord($s{1}) - 65536; 
  9.     if ($asc>=-20319 && $asc<=-20284) return 'A'
  10.     if ($asc>=-20283 && $asc<=-19776) return 'B'
  11.     if ($asc>=-19775 && $asc<=-19219) return 'C'
  12.     if ($asc>=-19218 && $asc<=-18711) return 'D'
  13.     if ($asc>=-18710 && $asc<=-18527) return 'E'
  14.     if ($asc>=-18526 && $asc<=-18240) return 'F'
  15.     if ($asc>=-18239 && $asc<=-17923) return 'G'
  16.     if ($asc>=-17922 && $asc<=-17418) return 'H'
  17.     if ($asc>=-17417 && $asc<=-16475) return 'J'
  18.     if ($asc>=-16474 && $asc<=-16213) return 'K'
  19.     if ($asc>=-16212 && $asc<=-15641) return 'L'
  20.     if ($asc>=-15640 && $asc<=-15166) return 'M'
  21.     if ($asc>=-15165 && $asc<=-14923) return 'N'
  22.     if ($asc>=-14922 && $asc<=-14915) return 'O'
  23.     if ($asc>=-14914 && $asc<=-14631) return 'P'
  24.     if ($asc>=-14630 && $asc<=-14150) return 'Q'
  25.     if ($asc>=-14149 && $asc<=-14091) return 'R'
  26.     if ($asc>=-14090 && $asc<=-13319) return 'S'
  27.     if ($asc>=-13318 && $asc<=-12839) return 'T'
  28.     if ($asc>=-12838 && $asc<=-12557) return 'W'
  29.     if ($asc>=-12556 && $asc<=-11848) return 'X'
  30.     if ($asc>=-11847 && $asc<=-11056) return 'Y'
  31.     if ($asc>=-11055 && $asc<=-10247) return 'Z'
  32.     return null; 
  33. }  

例如:echo getFirstCharter("程序員3aj.cn"); // 結果將輸出:C

實例二,代碼如下:

  1. <?php 
  2.  
  3. $limit=array//gb2312 拼音排序 
  4.     array(45217,45252), //A 
  5.     array(45253,45760), //B 
  6.     array(45761,46317), //C 
  7.     array(46318,46825), //D 
  8.     array(46826,47009), //E 
  9.     array(47010,47296), //F 
  10.     array(47297,47613), //G 
  11.     array(47614,48118), //H 
  12.     array(0,0),         //I 
  13.     array(48119,49061), //J 
  14.     array(49062,49323), //K 
  15.     array(49324,49895), //L 
  16.     array(49896,50370), //M 
  17.     array(50371,50613), //N 
  18.     array(50614,50621), //O 
  19.     array(50622,50905), //P 
  20.     array(50906,51386), //Q 
  21.     array(51387,51445), //R 
  22.     array(51446,52217), //S 
  23.     array(52218,52697), //T 
  24.     array(0,0),         //U 
  25.     array(0,0),         //V 
  26.     array(52698,52979), //W 
  27.     array(52980,53688), //X 
  28.     array(53689,54480), //Y 
  29.     array(54481,55289), //Z 
  30. ); 
  31.  
  32. $str="A:這是一個測試程序1"
  33. $str= iconv("UTF-8","gb2312"$str); 
  34. echo $str."</br>"
  35. $i=0; 
  36. while($i<strlen($str) ) { 
  37.     $tmp=bin2hex(substr($str,$i,1)); 
  38.     if($tmp>='B0'){ //漢字的開始 
  39.         $t=getLetter(hexdec(bin2hex(substr($str,$i,2)))); 
  40.         printf("%c",$t==-1 ? '*' : $t ); 
  41.         $i+=2; 
  42.     } 
  43.     else
  44.         printf("%s",substr($str,$i,1)); 
  45.         $i++; 
  46.     } 
  47.  
  48. function getLetter($num){ 
  49.     global $limit
  50.     $char_index=65; 
  51.     foreach($limit as $k=>$v){ 
  52.         if($num>=$v[0] && $num<=$v[1]){ 
  53.             $char_index+=$k
  54.             return $char_index
  55.         } 
  56.     } 
  57.     return -1; 
  58. ?>  

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 叙永县| 乌拉特前旗| 治县。| 金寨县| 浪卡子县| 中方县| 南城县| 建平县| 南京市| 合肥市| 佛山市| 抚远县| 夹江县| 盐亭县| 乌鲁木齐县| 怀仁县| 哈密市| 保靖县| 五大连池市| 确山县| 新郑市| 邓州市| 阜阳市| 安岳县| 金秀| 绥芬河市| 秦安县| 洮南市| 嘉荫县| 临澧县| 高邑县| 海安县| 永安市| 高阳县| 芦山县| 临江市| 元江| 星座| 顺昌县| 张掖市| 天全县|