PHP implementation get the first letter of Chinese characters two examples _php tutorial

Source: Internet
Author: User
To get the first letter of the method there are many, this we generally want to divide the Chinese characters and then converted to pinyin, and then use SUBSTR to take the first letter, below I found online two instances, different people together to see it.

Example 1

The main functions are: clear function, easy to modify maintenance and extension; English strings: Unchanged return (including numbers); Chinese string: Returns the first character of the Pinyin; mixed string: Returns the first character of the pinyin and English. The algorithm uses a two-point method to find and fix the error of the previous letter Z reading into Y. Good things to collect, so leave a mark here, for posterity textual research!

The code is as follows Copy Code

/**
* Fix the method of finding binary method
* Chinese Pinyin first Letter tool class
* Note: The English string: Unchanged returns (including numbers) eg. abc123 = abc123
* Chinese string: Returns the pinyin 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 (' Ah, just the hungry fly just fine I saw you oh flat to people is he UV I want to one in ');
*/
Class Str2py
{
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, specifying the 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 arrays (Chinese characters or one character units)
*
* @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 whether 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 Chinese characters 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 ', $str);
}
$words = $this->_cutword ($STR);
foreach ($words as $word)
{
if ($this->_isascii ($word)) {/* Non-Chinese */
$result [] = $word;
Continue
}
$code = Ord (substr ($word, 0,1)) * + ord (substr ($word, 1, 1));
/* Get pinyin initials a--z*/
if ($i = $this->_search ($code))! =-1) {
$result [] = $this->_pinyins[$i];
}
}
Return Strtoupper (Implode (", $result));
}
Private Function _getchar ($ASCII)
{
if ($ascii >= && $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 */
}
}

/**
* Find the phonetic characters (gb2312) corresponding to the required Chinese characters (binary method)
*
* @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;
}
}
}
}
?>

Example 2

Take the ASC range of the Chinese character and return the first letter of the Chinese character.

The code is as follows Copy Code

function Getfirstchar ($s 0) {
$fchar = Ord ($s 0{0});
if ($fchar >= ord ("A") and $fchar <= ord ("Z")) return Strtoupper ($s 0{0});
$s 1 = iconv ("UTF-8", "gb2312", $s 0);
$s 2 = iconv ("gb2312", "UTF-8", $s 1);
if ($s 2 = = $s 0) {$s = $s 1;} else{$s = $s 0;}
$ASC = Ord ($s {0}) * + ord ($s {1})-65536;
if ($asc >= -20319 and $asc <= -20284) return "A";
if ($asc >= -20283 and $asc <= -19776) return "B";
if ($asc >= -19775 and $asc <= -19219) return "C";
if ($asc >= -19218 and $asc <= -18711) return "D";
if ($asc >= -18710 and $asc <= -18527) return "E";
if ($asc >= -18526 and $asc <= -18240) return "F";
if ($asc >= -18239 and $asc <= -17923) return "G";
if ($asc >= -17922 and $asc <= -17418) return "I";
if ($asc >= -17417 and $asc <= -16475) return "J";
if ($asc >= -16474 and $asc <= -16213) return "K";
if ($asc >= -16212 and $asc <= -15641) return "L";
if ($asc >= -15640 and $asc <= -15166) return "M";
if ($asc >= -15165 and $asc <= -14923) return "N";
if ($asc >= -14922 and $asc <= -14915) return "O";
if ($asc >= -14914 and $asc <= -14631) return "P";
if ($asc >= -14630 and $asc <= -14150) return "Q";
if ($asc >= -14149 and $asc <= -14091) return "R";
if ($asc >= -14090 and $asc <= -13319) return "S";
if ($asc >= -13318 and $asc <= -12839) return "T";
if ($asc >= -12838 and $asc <= -12557) return "W";
if ($asc >= -12556 and $asc <= -11848) return "X";
if ($asc >= -11847 and $asc <= -11056) return "Y";
if ($asc >= -11055 and $asc <= -10247) return "Z";
return null;
}


function Pinyin1 ($zh) {
$ret = "";
$s 1 = iconv ("UTF-8", "gb2312", $zh);
$s 2 = iconv ("gb2312", "UTF-8", $s 1);
if ($s 2 = = $zh) {$zh = $s 1;}
for ($i = 0; $i < strlen ($zh); $i + +) {
$s 1 = substr ($zh, $i, 1);
$p = Ord ($s 1);
if ($p > 160) {
$s 2 = substr ($zh, $i ++,2);
$ret. = Getfirstchar ($s 2);
}else{
$ret. = $s 1;
}
}
return $ret;
}
echo "This is the Chinese string
";
Echo pinyin1 (' This is a Chinese string ');

?>

http://www.bkjia.com/PHPjc/632803.html www.bkjia.com true http://www.bkjia.com/PHPjc/632803.html techarticle to get the first letter of the method there are many, this we generally want to divide the Chinese characters and then converted to pinyin, and then use SUBSTR to take the first letter, below I found online two real ...

  • 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.