Php-tools for initials of Chinese characters and pinyin

Source: Internet
Author: User
: This article mainly introduces the php- 文 tool. For more information about PHP tutorials, see.
 Abc123 * Chinese string: return the first character of pinyin, eg. test string => CSZFC * English-Chinese mix string: returns the first character of pinyin and the English eg. I j => WIWJ * eg. * $ py = new str2PY (); ** $ result = $ py-> getInitials ('Jay Chou '); ** // get the first letter * $ result = $ py-> getFirstString ('ABC'); // A * $ resutl = $ py-> getFirstString ("Jay Chou "); // Z **/class str2py {private $ _ pinyins = array (176161 => 'A', 176197 => 'B', 178193 => 'C ', 180238 => 'D', 182234 => 'e', 183162 => 'F', 184193 => 'G', 185254 => 'h', 1 87247 => '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, specifying the required encoding default: UTF-8 * supports UTF-8, gb2312 ** @ param unknown_type $ charset */public function _ construct ($ charset = 'utf-8') {$ this-> _ Charset = $ charset ;} /*** Chinese character 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);} els E {$ result. = substr ($ str, $ I, 1) ;}} if (ord (substr ($ str, $ I, 1)> 129) {$ I ++ ;}} return $ result;}/*** the string is partitioned into arrays (in Chinese characters or characters) ** @ 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 ;} /*** determine whether the 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 three characters of the string are ascii characters ** @ p Aram 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 first character of a Chinese string ** @ param string $ str * @ return string */public function getInitials ($ 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 ', $ st R) ;}$ 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 initial A--Z */if ($ I = $ this-> _ search ($ code ))! =-1) {$ result [] = $ this-> _ pinyins [$ I] ;}} return strtoupper (implode ('', $ result ));} /*** get the first letter of wangtianbao * @ param string $ str * @ return string */public function getFirstString ($ str) {// Convert Chinese characters to $ new_string = $ this-> getInitials ($ str); if (empty ($ new_string) {return '';} else {return strtoupper (substr ($ new_string, 0, 1) ;}} private function _ getChar ($ ascii) {if ($ ascii> = 48 & $ Ascii <= 57) {return chr ($ ascii);/* number */} elseif ($ ascii> = 65 & $ ascii <= 90) {return chr ($ ascii);/* A--Z */} elseif ($ ascii> = 97 & $ ascii <= 122) {return chr ($ ascii-32 ); /* a -- z */} else {return '-';/* other */}/*** search for the required Chinese character internal code (gb2312) corresponding Pinyin characters (bipartite) ** @ param int $ code * @ return int */private function _ search ($ code) {$ data = array_keys ($ this-> _ pinyins); $ lower = 0; $ uppe R = 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 ;}}}}

The above introduces the php-Chinese character and Pinyin initials tool class, including some content, hope to be helpful to friends who are interested in PHP tutorials.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.