Example of converting php Chinese characters to pinyin

Source: Internet
Author: User
This article mainly introduces the example of converting Chinese characters into pinyin in php. For more information, see The code is as follows:
Class Helper_Spell {
Public $ spellArray = array ();

Static public function getArray (){
Return unserialize(file_get_contents('pytable_without_tune.txt '));
}
/**
* @ Desc get the first letter of the string
* @ Param $ string the string to be converted
* @ Param $ whether isOne is the first letter
* @ Param $ whether the upper is converted to uppercase
* @ Return string
*
* For example, getChineseFirstChar ('I am the author') must start with all letters and lowercase letters.
* Return "wo"
*
* For example, getChineseFirstChar ('I am the author', true) first letter + lower case
* Return "w"
*
* For example: getChineseFirstChar ('I am the author', true, true) first letter + uppercase
* Return "W"
*
* For example, getChineseFirstChar ('I am the author', false, true) must start with all letters and uppercase letters.
* Return "WO"
*/
Static public function getChineseFirstChar ($ string, $ isOne = false, $ upper = false ){
$ SpellArray = self: getArray ();
$ Str_arr = self: utf8_str_split ($ string, 1); // splits a string into an array.

If (preg_match ('/^ [\ x {4e00}-\ x {9fa5}] + $/U', $ str_arr [0]) {// You can check whether the string is a Chinese character.
$ Chinese = $ spellArray [$ str_arr [0];
$ Result = $ chinese [0];
} Else {
$ Result = $ str_arr [0];
}

$ Result = $ isOne? Substr ($ result, 0, 1): $ result;

Return $ upper? Strtoupper ($ result): $ result;
}

/**
* @ Desc converts a string to a pinyin string
* @ Param $ string Chinese character string
* @ Param $ whether the upper is capitalized
* @ Return string
*
* Example: getChineseChar ('I am the author'); all strings + lower case
* Return "wo shi zuo zhe"
*
* For example: getChineseChar ('I am the author', true); first letter + lower case
* Return "w s z"
*
* For example: getChineseChar ('I am the author', true, true); initial letter + uppercase letter
* Return "w s z"
*
* For example: getChineseChar ('I am the author', false, true); initial letter + uppercase letter
* Return "wo shi zuo zhe"
*/
Static public function getChineseChar ($ string, $ isOne = false, $ upper = false ){
Global $ spellArray;
$ Str_arr = self: utf8_str_split ($ string, 1); // splits a string into an array.
$ Result = array ();
Foreach ($ str_arr as $ char)
{
If (preg_match ('/^ [\ x {4e00}-\ x {9fa5}] + $/U', $ char ))
{
$ Chinese = $ spellArray [$ char];
$ Chinese = $ chinese [0];
} Else {
$ Chinese = $ char;
}
$ Chinese = $ isOne? Substr ($ chinese, 0, 1): $ chinese;
$ Result [] = $ upper? Strtoupper ($ chinese): $ chinese;
}
Return implode ('', $ result );
}
/**
* @ Desc converts a string to an array
* @ Param $ str the array to be converted
* @ Param $ split_len
* @ Return array
*/
Private function utf8_str_split ($ str, $ split_len = 1 ){

If (! Preg_match ('/^ [0-9] + $/', $ split_len) | $ split_len <1 ){
Return FALSE;
}

$ Len = mb_strlen ($ str, 'utf-8 ');

If ($ len <= $ split_len ){
Return array ($ str );
}
Preg_match_all ('/. {'. $ split_len. '} | [^ \ x00] {1 ,'. $ split_len. '} $/us', $ str, $ ar );

Return $ ar [0];
}
}

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.