1. 程式人生 > >獲取漢字拼音首字母類封裝

獲取漢字拼音首字母類封裝

1、程式碼如下:

 1 <?php
 2 /**
 3     * @file Pinyin.php
 4     * @author whx https://www.cnblogs.com/whx-blogs/
 5     * @date 2018/12/03 18:32:18
 6     * @brief
 7     *
 8 **/
 9 class PinyinClass {
10     /**
11      * [pinyin 轉換漢字的拼音首字母]
12      * @param  [type] $zh [漢字]
13      * @return [type]     [返回小寫字母]
14 */ 15 public function pinyin($zh){ 16 $ret = ""; 17 $s1 = iconv("UTF-8","gb2312", $zh); 18 $s2 = iconv("gb2312","UTF-8", $s1); 19 if($s2 == $zh){$zh = $s1;} 20 for($i = 0; $i < strlen($zh); $i++){ 21 $s1 = substr($zh,$i,1); 22 $p
= ord($s1); 23 if($p > 160){ 24 $s2 = substr($zh,$i++,2); 25 $ret .= $this->getfirstchar($s2); 26 }else{ 27 $ret .= $s1; 28 } 29 } 30 return strtolower($ret); 31 } 32 33 /** 34
* @name: getfirstchar 35 * @description: 獲取漢子首字母 36 * @param: string 37 * @return: mixed 38 * @author: 39 * @create: 2014-09-17 21:46:52 40 **/ 41 private function getfirstchar($s0){ 42 $fchar = ord($s0{0}); 43 if($fchar >= ord("A") and $fchar <= ord("z") )return strtoupper($s0{0}); 44 $s1 = $this->get_encoding($s0,'GB2312'); 45 $s2 = $this->get_encoding($s1,'UTF-8'); 46 if($s2 == $s0){$s = $s1;}else{$s = $s0;} 47 $asc = ord($s{0}) * 256 + ord($s{1}) - 65536; 48 if($asc >= -20319 and $asc <= -20284) return "A"; 49 if($asc >= -20283 and $asc <= -19776) return "B"; 50 if($asc >= -19775 and $asc <= -19219) return "C"; 51 if($asc >= -19218 and $asc <= -18711) return "D"; 52 if($asc >= -18710 and $asc <= -18527) return "E"; 53 if($asc >= -18526 and $asc <= -18240) return "F"; 54 if($asc >= -18239 and $asc <= -17923) return "G"; 55 if($asc >= -17922 and $asc <= -17418) return "I"; 56 if($asc >= -17417 and $asc <= -16475) return "J"; 57 if($asc >= -16474 and $asc <= -16213) return "K"; 58 if($asc >= -16212 and $asc <= -15641) return "L"; 59 if($asc >= -15640 and $asc <= -15166) return "M"; 60 if($asc >= -15165 and $asc <= -14923) return "N"; 61 if($asc >= -14922 and $asc <= -14915) return "O"; 62 if($asc >= -14914 and $asc <= -14631) return "P"; 63 if($asc >= -14630 and $asc <= -14150) return "Q"; 64 if($asc >= -14149 and $asc <= -14091) return "R"; 65 if($asc >= -14090 and $asc <= -13319) return "S"; 66 if($asc >= -13318 and $asc <= -12839) return "T"; 67 if($asc >= -12838 and $asc <= -12557) return "W"; 68 if($asc >= -12556 and $asc <= -11848) return "X"; 69 if($asc >= -11847 and $asc <= -11056) return "Y"; 70 if($asc >= -11055 and $asc <= -10247) return "Z"; 71 return null; 72 } 73 74 /** 75 * @name: get_encoding 76 * @description: 自動檢測內容編碼進行轉換 77 * @param: string data 78 * @param: string to 目標編碼 79 * @return: string 80 **/ 81 private function get_encoding($data,$to){ 82 $encode_arr=array('UTF-8','ASCII','GBK','GB2312','BIG5','JIS','eucjp-win','sjis-win','EUC-JP'); 83 $encoded=mb_detect_encoding($data, $encode_arr); 84 $data = mb_convert_encoding($data,$to,$encoded); 85 return $data; 86 } 87 88 } 89 90 $Obj = new PinyinClass(); 91 $str = '我是中國人'; 92 echo "漢字:" . $str . "<br>"; 93 echo '拼音首字母:' . $Obj->pinyin($str);

2、執行結果: