php-Chinese Pinyin first letter tool class, php-Chinese character tool class
Php/** * Chinese Pinyin First Letter tool class * Note: English strings: Unchanged returns (including numbers) eg. abc123 = abc123* Chinese string: Returns the phonetic first character eg. test string = cszfc* Chinese-English mixed string: Returns the pinyin first character and English eg. I i i j = wiwj* eg.* $py = new Str2py (); * * $result = $py->getinitials (' Jay Chou '); * *//Get first letter * $result = $py->GETF Irststring (' abc '); A * $resutl = $py->getfirststring ("Jay Chou"); z**/classstr2py{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 ', ); Private $_charset=NULL; /** * constructor, specify required encoding Default:utf-8 * Support UTF-8, gb2312 * * @param unknown_type $charset*/ Public function__construct ($charset= ' Utf-8 ') { $this->_charset =$charset; } /** * Chinese string substr * * @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<$strlen;$i++) { if($i>=$start&&$i< ($start+$len)) { 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; } /** * string is divided into an array (kanji or a character unit) * * @param string $str * @return Array*/ Private function_cutword ($str) { $words=Array(); while($str!= "") { if($this->_isascii ($str)) {/*Non-Chinese*/ $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; } /** * Determines if a character is an ASCII character * * @param string $char * @return bool*/ Private function_isascii ($char) { return(Ord(substr($char, 0, 1)) < 160 ); } /** * Determines whether the first 3 characters of a string are ASCII characters * * @param string $STR * @return bool*/ Private function_isasciis ($str) { $len=strlen($str) >= 3? 3:2; $chars=Array(); for($i= 1;$i<$len-1;$i++) { $chars[] =$this->_isascii ($str[$i]) ? ' Yes ': ' No '; } $result=array_count_values($chars); if(Empty($result[' No '])) { return true; } return false; } /** * get the pinyin first character of the Chinese text string * * @param string $str * @return String*/ Public functionGetinitials ($str) { if(Empty($str)) return''; if($this->_isascii ($str[0]) &&$this->_isasciis ($str)) { return $str; } $result=Array(); if($this->_charset = = ' Utf-8 ') { $str=Iconv(' Utf-8 ', ' gb2312 ',$str); } $words=$this->_cutword ($str); foreach($words as $word) { if($this->_isascii ($word)) {/*Non-Chinese*/ $result[] =$word; Continue; } $code=Ord(substr($word, 0, 1)) * 1000 +Ord(substr($word, 1, 1)); /*get pinyin first letter A--z*/ if(($i=$this->_search ($code))! =-1) { $result[] =$this->_pinyins[$i]; } } return Strtoupper(implode('',$result)); } /** * 20140624 Wangtianbao Get the first letter * @param string $str * @return String*/ Public functionGetfirststring ($str) { //convert Chinese into letters first $new _string=$this->getinitials ($str); if(Empty($new _string)) { return''; } Else { return Strtoupper(substr($new _string, 0, 1)); } } Private function_getchar ($ascii) { if($ascii>= &&$ascii<= 57) { return CHR($ascii);/*Digital*/ } ElseIf($ascii>= &&$ascii<= 90) { return CHR($ascii);/*a--z*/ } ElseIf($ascii>= &&$ascii<= 122) { return CHR($ascii-32);/*a--z*/ } Else { return'-';/*other*/ } } /** * Find the required Chinese characters (gb2312) corresponding pinyin characters (dichotomy) * * @param int $code * @return int*/ Private function_search ($code) { $data=Array_keys($this-_pinyins); $lower= 0; $upper=sizeof($data)-1; $middle= (int)round(($lower+$upper)/2); if($code<$data[0]) return-1; for (;;) { if($lower>$upper) { return $data[$lower-1]; } $tmp= (int)round(($lower+$upper)/2); if(!isset($data[$tmp])) { return $data[$middle]; } Else { $middle=$tmp; } if($data[$middle] <$code) { $lower= (int)$middle+ 1; } Else if($data[$middle] ==$code) { return $data[$middle]; } Else { $upper= (int)$middle-1; } } }}
http://www.bkjia.com/PHPjc/925549.html www.bkjia.com true http://www.bkjia.com/PHPjc/925549.html techarticle php-Chinese Pinyin first letter tool class, php-Chinese character tool class? PHP/* * Chinese Pinyin First Letter tool class * Note: English strings: Unchanged returns (including numbers) eg. abc123 = abc123* text ...