Share a piece of php-made Chinese phonetic Alphabet tool class _php example

Source: Internet
Author: User
Tags chr ord

Code is very simple, here is not much BB, we look at the comments, comments are not understand the small partners, to let go of php!!!

Copy Code code as follows:

<?php
/**
* Chinese Pinyin first Letter tool class
* Note: English string: Unchanged return (including number) eg. abc123 => abc123
* Chinese character string: Return phonetic first characters eg. Test string => CSZFC
* Chinese and English mixed string: Return the phonetic first character and the 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 encoding required Default:utf-8
* Support 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);
}
Else
{
$result. = substr ($str, $i, 1);
}
}
if (Ord (substr ($str, $i, 1)) > 129)
{
$i + +;
}
}
return $result;
}
/**
* The string is divided into an array (Chinese character or one 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;
}
/**
* Determine if the character is an ASCII character
*
* @param string $char
* @return BOOL
*/
Private Function _isascii ($char)
{
Return (Ord (substr ($char, 0, 1)) < 160);
}
/**
* Determine whether the first 3 characters of the 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 phonetic first character of the Chinese text 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)) * 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 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); /* 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 character code (gb2312) corresponding phonetic 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 Chinese characters is currently almost no project need to use the function, here to recommend the higher efficiency of the code, but also in my project use, small partners such as the discovery of problems, please leave a message, we common progress

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.