Share a php-made pinyin first letter tool class, _php tutorial

Source: Internet
Author: User

Share a php-made Chinese pinyin first letter tool class,


The code is very simple, here is not many BB, we read the comments, comments do not understand the small partner, beg to leave php!!!

Copy CodeThe code is as follows:
<?php
/**
* 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 (' Jay Chou ');
*
*//Get First letter
* $result = $py->getfirststring (' abc '); A
* $resutl = $py->getfirststring ("Jay Chou"); Z
*
*/
Class Str2py
{
Private $_pinyins = Array (
= ' A ',
= ' B ',
= ' C ',
= ' D ',
= ' E ',
= ' F ',
= ' G ',
= ' H ',
= ' J ',
= ' K ',
= ' L ',
= ' M ',
= = ' N ',
= ' O ',
= ' P ',
= ' Q ',
= ' R ',
= ' S ',
= ' T ',
= ' W ',
= ' X ',
= ' Y ',
= ' 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));
}
/**
* 20140624 Wangtianbao Get first letter
* @param string $str
* @return String
*/
Public Function getfirststring ($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); /* Number */
}
ElseIf ($ascii >= && $ascii <= 90)
{
return Chr ($ASCII); /* A--z */
}
ElseIf ($ascii >= && $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;
}
}
}
}

Take the first letter of the Chinese character is currently almost no project need to use the function, here is recommended is the high efficiency of the Code, but also in my project use, small partners such as found problems, please leave a message, we progress together

http://www.bkjia.com/PHPjc/926362.html www.bkjia.com true http://www.bkjia.com/PHPjc/926362.html techarticle share a piece of php production of Chinese Pinyin first letter tool class, the code is very simple, here is not many BB, we read the comments, comments do not understand the small partner, beg to leave php!!! Copy generation ...

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