Example of converting php Chinese characters to PinYin
This article mainly introduces the example of converting Chinese characters into pinyin in php. For more information, see
The Code is as follows:
<? Php
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];
}
}