这篇文章主要介绍了php获取中文拼音首字母类和函数,需要的朋友可以参考下
一、公司同事整理的类,挺实用的.相信拿出来分享下他不会介意的o(∩_∩)o.不过如果首字母是数字或英文会有些问题.
复制代码 代码如下:
/**
* helper_spell 汉字拼音首字母工具类
*
* @category helper
* @package helper_spell
* @author lancer
* @version 1.0
* @see translation_big2gb
*/
class helper_spell {
/**
* $_pinyins
* @var array
* @access private
*/
private $_pinyins = array(
176161 => 'a',
176197 => 'b',
178193 => 'c',
180238 => 'd',
182234 => 'e',
183162 => 'f',
184193 => 'g',
185254 => 'h',
187247 => 'j',
191166 => 'k',
192172 => 'l',
194232 => 'm',
196195 => 'n',
197182 => 'o',
197190 => 'p',
198218 => 'q',
200187 => 'r',
200246 => 's',
203250 => 't',
205218 => 'w',
206244 => 'x',
209185 => 'y',
212209 => 'z',
215249 => 'z',
);
/**
* $_charset
* @var string
* @access private
*/
private $_charset = null;
/**
* __construct 构造函数, 指定需要的编码 default: utf-8 支持utf-8, gb2312
*
* @param unknown_type $charset
*/
public function __construct( $charset = 'utf-8' ) {
$this->_charset = $charset;
}
/**
* getinitialsfirst 返回首个汉字的拼音
*
* @access public
* @static
* @param string $str
* @return string
* @example helper_spell::getinitialsfirst('我的爱'); => w
*/
public static function getinitialsfirst( $str, $charset = 'utf-8' ) {
$chars = array(
'a','b','c','d','e','f',
'g','h','i','j','k','l',
'm','n','o','p','q','r',
's','t','u','v','w','x',
'y','z');
$string = self::getinitials( $str );
$length = strlen($string);
for($i=0; $i if ( in_array( $string{$i}, $chars ) ) {
return $string{$i};
}
}
return '*';
}
/**
* getinitials 返回拼音组合
*
* @access public
* @static
* @param string $str
* @return string
* @example helper_spell::getinitials('我的爱'); => wda
*/
public static function getinitials( $str, $charset = 'utf-8' ) {
$instance = new helper_spell( $charset );
return $instance->_getinitials( $str );
}
/**
* _getinitials 获取中文字串的拼音首字符
* 注:英文的字串:不变返回(包括数字) eg .abc123 => abc123
* 中文字符串:返回拼音首字符 eg. 王小明 => wxm
* 中英混合串: 返回拼音首字符和英文 eg. 我i我j => wiwj
*
* @access private
* @param string $str
* @return string
*/
private function _getinitials( $str, $translation=true ){
if ( empty($str) ) return '';
if ( $this->_isascii($str[0]) && $this->_isasciis( $str ))
return $str;
if ( $translation )
$str = translation_big2gb::big2gb( $str );
$result = array();
if ( $this->_charset == 'utf-8' ){
//ignore很重要,加上这个就可以是iconv()函数忽略错误,继续执行
$str = iconv( 'utf-8', 'gbk//ignore', $str );
}
$words = $this->_cutword( $str );
foreach ( $words as $word ) {
if ( $this->_isascii($word) ) {//非中文
$result[] = $word;
continue;
}
$code = ( ord(substr($word,0,1)) ) * 1000 + (ord(substr($word,1,1)));
//获取拼音首字母a--z
if ( ($i = $this->_search($code)) != -1 ){
$result[] = $this->_pinyins[$i];
}
}
return strtoupper(implode('', $result));
}
/**
* _msubstr 获取中文字符串
*
* @access private
* @param string $str
* @param int $start
* @param int $len
* @return string
*/
private function _msubstr ($str, $start, $len) {
$start = $start * 2;
$len = $len * 2;
$strlen = strlen($str);
$result = '';
for ( $i = 0; $i if ( $i >= $start && $i if ( ord(substr($str, $i, 1)) > 129 ) $result .= substr($str, $i, 2);
else $result .= substr($str, $i, 1);
}
if ( ord(substr($str, $i, 1)) > 129 ) $i++;
}
return $result;
}
/**
* _cutword 字符串切分为数组 (汉字或者一个字符为单位)
*
* @access private
* @param string $str
* @return array
*/
private function _cutword( $str ) {
$words = array();
while ( $str != ) {
if ( $this->_isascii($str) ) {//非中文
$words[] = $str[0];
$str = substr( $str, strlen($str[0]) );
} else {
$word = $this->_msubstr( $str, 0, 1 );
$words[] = $word;
$str = substr( $str, strlen($word) );
}
}
return $words;
}
/**
* _isascii 判断字符是否是ascii字符
*
* @access private
* @param string $char
* @return bool
*/
private function _isascii( $char ) {
return ( ord( substr($char,0,1) ) }
/**
* _isasciis 判断字符串前3个字符是否是ascii字符
*
* @access private
* @param string $str
* @return bool
*/
private function _isasciis( $str ) {
$len = strlen($str) >= 3 ? 3: 2;
$chars = array();
for( $i = 1; $i $chars[] = $this->_isascii( $str[$i] ) ? 'yes':'no';
}
$result = array_count_values( $chars );
if ( empty($result['no']) ){
return true;
}
return false;
}
/**
* _getchar 通过asc码返回字母或者数字
*
* @access private
* @param string $ascii
* @return string
*/
private function _getchar( $ascii ){
if ( $ascii >= 48 && $ascii return chr($ascii); //数字
} elseif ( $ascii>=65 && $ascii return chr($ascii); // a--z
} elseif ($ascii>=97 && $ascii return chr($ascii-32); // a--z
} else {
return '~'; //其他
}
}
/**
* _search 查找需要的汉字内码(gb2312) 对应的拼音字符(二分法)
*
* @access private
* @param int $code
* @return int
*/
private function _search( $code ) {
$data = array_keys($this->_pinyins);
$lower = 0;
$upper = sizeof($data)-1;
// 排除非一级汉字
if ($code $data[23]) return -1;
for (;;) {
if ( $lower > $upper ){
return $data[$lower-1];
}
$middle = (int) round(($lower + $upper) / 2);
if ( !isset($data[$middle]) ) {
return -1;
}
if ( $data[$middle] $lower = (int)$middle + 1;
} else if ( $data[$middle] == $code ) {
return $data[$middle];
} else {
$upper = (int)$middle - 1;
}
}// end for
}
}
二、用来得到中文的首字母
这个是将中文转换为拼音的类:charset
复制代码 代码如下: