[PHP] 取得汉字首字母(类) →→→→→进入此内容的聊天室

来自 , 2019-08-25, 写在 PHP, 查看 106 次.
URL http://www.code666.cn/view/6271faad
  1. //取得汉字首字母
  2. /**
  3. * 修复二分法查找方法
  4. * 汉字拼音首字母工具类
  5. *  注: 英文的字串:不变返回(包括数字)    eg .abc123 => abc123
  6. *      中文字符串:返回拼音首字符        eg. 测试字符串 => CSZFC
  7. *      中英混合串: 返回拼音首字符和英文   eg. 我i我j => WIWJ
  8. *  //使用方法:
  9. *  $py = new str2PY();
  10. *  $result = $py->getInitials('需要生成首字母的字符串');
  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 ( empty($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 ( empty($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.      * 查找需要的汉字内码(gb2312) 对应的拼音字符( 二分法 )
  169.      *
  170.      * @param int $code
  171.      * @return int
  172.      */
  173.     private function _search( $code )
  174.     {
  175.         $data = array_keys($this->_pinyins);
  176.         $lower = 0;
  177.         $upper = sizeof($data)-1;
  178.   $middle = (int) round(($lower + $upper) / 2);
  179.         if ( $code < $data[0] ) return -1;
  180.         for (;;) {
  181.             if ( $lower > $upper ){
  182.                 return $data[$lower-1];
  183.             }
  184.             $tmp = (int) round(($lower + $upper) / 2);
  185.             if ( !isset($data[$tmp]) ){
  186.     return $data[$middle];
  187.             }else{
  188.     $middle = $tmp;
  189.    }
  190.             if ( $data[$middle] < $code ){
  191.                 $lower = (int)$middle + 1;
  192.             }else if ( $data[$middle] == $code ) {
  193.                 return $data[$middle];
  194.             }else{
  195.                 $upper = (int)$middle - 1;
  196.             }
  197.         }
  198.     }
  199. }

回复 "取得汉字首字母(类)"

这儿你可以回复上面这条便签

captcha