首页 > 开发 > PHP > 正文

php实现获取汉字的首字母两个实例

2024-05-04 21:58:06
字体:大 中 小
来源:转载
供稿:网友

要获取汉字首字母的方法有很多,这个我们一般要把汉字分出来然后转换成拼音,然后再利用substr取第一个字母了,下面我网上找到两个实例,各有千秋大家一起来看看吧.

例1,主要功能是:功能明确,易于修改维护和扩展;英文的字串:不变返回(包括数字);中文字符串:返回拼音首字符; 中英混合串:返回拼音首字符和英文,该算法采用了二分法查找,修复了之前字母Z读取成Y的错误,代码如下:

  1. <?php  
  2.  /** 
  3. * 修复二分法查找方法 
  4. * 汉字拼音首字母工具类 
  5. *  注: 英文的字串:不变返回(包括数字)    eg .abc123 => abc123 
  6. *      中文字符串:返回拼音首字符        eg. 测试字符串 => CSZFC 
  7. *      中英混合串: 返回拼音首字符和英文   eg. 我i我j => WIWJ 
  8. *  eg. 
  9. *  $py = new str2PY(); 
  10. *  $result = $py->getInitials('啊吧才的饿飞就好i就看了吗你哦平去人是他uv我想一在'); 
  11. */ 
  12. class str2PY 
  13. { 
  14.     private $_pinyins = array( 
  15.         176161 => 'A', 
  16.         176197 => 'B', 
  17.         178193 => 'C', 
  18.         180238 => 'D', 
  19.         182234 => 'E', 
  20.         183162 => 'F', 
  21.         184193 => 'G', 
  22.         185254 => 'H', 
  23.         187247 => 'J', 
  24.         191166 => 'K', 
  25.         192172 => 'L', 
  26.         194232 => 'M', 
  27.         196195 => 'N', 
  28.         197182 => 'O', 
  29.         197190 => 'P', 
  30.         198218 => 'Q', 
  31.         200187 => 'R', 
  32.         200246 => 'S', 
  33.         203250 => 'T', 
  34.         205218 => 'W', 
  35.         206244 => 'X', 
  36.         209185 => 'Y', 
  37.         212209 => 'Z', 
  38.     ); 
  39.     private $_charset = null; 
  40.     /** 
  41.      * 构造函数, 指定需要的编码 default: utf-8 
  42.      * 支持utf-8, gb2312 
  43.      * 
  44.      * @param unknown_type $charset 
  45.      */ 
  46.     public function __construct( $charset = 'utf-8' ) 
  47.     { 
  48.         $this->_charset    = $charset; 
  49.     } 
  50.     /** 
  51.      * 中文字符串 substr 
  52.      * 
  53.      * @param string $str 
  54.      * @param int    $start 
  55.      * @param int    $len 
  56.      * @return string 
  57.      */ 
  58.     private function _msubstr ($str, $start, $len) 
  59.     { 
  60.         $start  = $start * 2; 
  61.         $len    = $len * 2; 
  62.         $strlen = strlen($str); 
  63.         $result = ''; 
  64.         for ( $i = 0; $i < $strlen; $i++ ) { 
  65.             if ( $i >= $start && $i < ($start + $len) ) { 
  66.                 if ( ord(substr($str, $i, 1)) > 129 ) $result .= substr($str, $i, 2); 
  67.                 else $result .= substr($str, $i, 1); 
  68.             } 
  69.             if ( ord(substr($str, $i, 1)) > 129 ) $i++; 
  70.         } 
  71.         return $result; 
  72.     } 
  73.     /** 
  74.      * 字符串切分为数组 (汉字或者一个字符为单位) 
  75.      * 
  76.      * @param string $str 
  77.      * @return array 
  78.      */ 
  79.     private function _cutWord( $str ) 
  80.     { 
  81.         $words = array(); 
  82.          while ( $str != "" ) 
  83.          { 
  84.             if ( $this->_isAscii($str) ) {/*非中文*/ 
  85.                 $words[] = $str[0]; 
  86.                 $str = substr( $str, strlen($str[0]) ); 
  87.             }else{ 
  88.                 $word = $this->_msubstr( $str, 0, 1 ); 
  89.                 $words[] = $word; 
  90.                 $str = substr( $str, strlen($word) ); 
  91.             } 
  92.          } 
  93.          return $words; 
  94.     } 
  95.     /** 
  96.      * 判断字符是否是ascii字符 
  97.      * 
  98.      * @param string $char 
  99.      * @return bool 
  100.      */ 
  101.     private function _isAscii( $char ) 
  102.     { 
  103.         return ( ord( substr($char,0,1) ) < 160 ); 
  104.     } 
  105.     /** 
  106.      * 判断字符串前3个字符是否是ascii字符 
  107.      * 
  108.      * @param string $str 
  109.      * @return bool 
  110.      */ 
  111.     private function _isAsciis( $str ) 
  112.     { 
  113.         $len = strlen($str) >= 3 ? 3: 2; 
  114.         $chars = array(); 
  115.         for( $i = 1; $i < $len -1; $i++ ){ 
  116.             $chars[] = $this->_isAscii( $str[$i] ) ? 'yes':'no'; 
  117.         } 
  118.         $result = array_count_values( $chars ); 
  119.         if ( emptyempty($result['no']) ){ 
  120.             return true; 
  121.         } 
  122.         return false; 
  123.     } 
  124.     /** 
  125.      * 获取中文字串的拼音首字符 
  126.      * 
  127.      * @param string $str 
  128.      * @return string 
  129.      */ 
  130.     public function getInitials( $str ) 
  131.     { 
  132.         if ( emptyempty($str) ) return ''; 
  133.         if ( $this->_isAscii($str[0]) && $this->_isAsciis( $str )){ 
  134.             return $str; 
  135.         } 
  136.         $result = array(); 
  137.         if ( $this->_charset == 'utf-8' ){ 
  138.             $str = iconv( 'utf-8', 'gb2312', $str ); 
  139.         } 
  140.         $words = $this->_cutWord( $str ); 
  141.         foreach ( $words as $word ) 
  142.         { 
  143.             if ( $this->_isAscii($word) ) {/*非中文*/ 
  144.                 $result[] = $word; 
  145.                 continue; 
  146.             } 
  147.             $code = ord( substr($word,0,1) ) * 1000 + ord( substr($word,1,1) ); 
  148.             /*获取拼音首字母A--Z*/ 
  149.             if ( ($i = $this->_search($code)) != -1 ){ 
  150.                 $result[] = $this->_pinyins[$i]; 
  151.             } 
  152.         } 
  153.         return strtoupper(implode('',$result)); 
  154.     } 
  155.     private function _getChar( $ascii ) 
  156.     { 
  157.         if ( $ascii >= 48 && $ascii <= 57){ 
  158.             return chr($ascii);  /*数字*/ 
  159.         }elseif ( $ascii>=65 && $ascii<=90 ){ 
  160.             return chr($ascii);   /* A--Z*/ 
  161.         }elseif ($ascii>=97 && $ascii<=122){ 
  162.             return chr($ascii-32); /* a--z*/ 
  163.         }else{ 
  164.             return '-'; /*其他*/ 
  165.         } 
  166.     } 
  167.  
  168.     /** 
  169.      * 查找需要的汉字内码(gb2312) 对应的拼音字符( 二分法 ) 
  170.      * 
  171.      * @param int $code 
  172.      * @return int 
  173.      */ 
  174.     private function _search( $code ) 
  175.     { 
  176.         $data = array_keys($this->_pinyins); 
  177.         $lower = 0; 
  178.         $upper = sizeof($data)-1; 
  179.   $middle = (int) round(($lower + $upper) / 2); 
  180.         if ( $code < $data[0] ) return -1; 
  181.         for (;;) { 
  182.             if ( $lower > $upper ){ 
  183.                 return $data[$lower-1]; 
  184.             } 
  185.             $tmp = (int) round(($lower + $upper) / 2); 
  186.             if ( !isset($data[$tmp]) ){ 
  187.     return $data[$middle]; 
  188.             }else{  
  189.     $middle = $tmp; 
  190.    } 
  191.             if ( $data[$middle] < $code ){ 
  192.                 $lower = (int)$middle + 1; 
  193.             }else if ( $data[$middle] == $code ) { 
  194.                 return $data[$middle]; 
  195.             }else{ 
  196.                 $upper = (int)$middle - 1; 
  197.             } 
  198.         } 
  199.     } 
  200. } 
  201. ?> 

例2,取汉字的asc区间然后返回汉字首字母了,代码如下:

  1. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  
  2. <?php   
  3. function getfirstchar($s0){     
  4.     $fchar = ord($s0{0});  
  5.     if($fchar >= ord("A") and $fchar <= ord("z") )return strtoupper($s0{0});  
  6.     $s1 = iconv("UTF-8","gb2312", $s0);  
  7.     $s2 = iconv("gb2312","UTF-8", $s1);  
  8.     if($s2 == $s0){$s = $s1;}else{$s = $s0;}  
  9.     $asc = ord($s{0}) * 256 + ord($s{1}) - 65536;  
  10.     if($asc >= -20319 and $asc <= -20284) return "A";  
  11.     if($asc >= -20283 and $asc <= -19776) return "B";  
  12.     if($asc >= -19775 and $asc <= -19219) return "C";  
  13.     if($asc >= -19218 and $asc <= -18711) return "D";  
  14.     if($asc >= -18710 and $asc <= -18527) return "E";  
  15.     if($asc >= -18526 and $asc <= -18240) return "F";  
  16.     if($asc >= -18239 and $asc <= -17923) return "G";  
  17.     if($asc >= -17922 and $asc <= -17418) return "I";  
  18.     if($asc >= -17417 and $asc <= -16475) return "J";  
  19.     if($asc >= -16474 and $asc <= -16213) return "K";  
  20.     if($asc >= -16212 and $asc <= -15641) return "L";  
  21.     if($asc >= -15640 and $asc <= -15166) return "M";  
  22.     if($asc >= -15165 and $asc <= -14923) return "N";  
  23.     if($asc >= -14922 and $asc <= -14915) return "O";  
  24.     if($asc >= -14914 and $asc <= -14631) return "P";  
  25.     if($asc >= -14630 and $asc <= -14150) return "Q";  
  26.     if($asc >= -14149 and $asc <= -14091) return "R";  
  27.     if($asc >= -14090 and $asc <= -13319) return "S";  
  28.     if($asc >= -13318 and $asc <= -12839) return "T";  
  29.     if($asc >= -12838 and $asc <= -12557) return "W";  
  30.     if($asc >= -12556 and $asc <= -11848) return "X";  
  31.     if($asc >= -11847 and $asc <= -11056) return "Y";  
  32.     if($asc >= -11055 and $asc <= -10247) return "Z";  
  33.     return null;  
  34. }  
  35.    
  36.    
  37. function pinyin1($zh){  
  38.     $ret = "";  
  39.     $s1 = iconv("UTF-8","gb2312", $zh);  
  40.     $s2 = iconv("gb2312","UTF-8", $s1);  
  41.     if($s2 == $zh){$zh = $s1;}  
  42.     for($i = 0; $i < strlen($zh); $i++){  
  43.         $s1 = substr($zh,$i,1);  
  44.         $p = ord($s1);  
  45.         if($p > 160){  
  46.             $s2 = substr($zh,$i++,2);  
  47.             $ret .= getfirstchar($s2);  
  48.         }else{  
  49.             $ret .= $s1;  
  50.         }  
  51.     }  
  52.     return $ret;  
  53. }  
  54. echo "这是中文字符串<br/>";  
  55. echo pinyin1('这是中文字符串'); 
  56. ?> 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表