PHP kanji to Pinyin example _php tutorial

Source: Internet
Author: User
Copy CodeThe 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 convert
* @param $isOne whether to take the first letter
* @param $upper converted to uppercase
* @return String
*
* Example: Getchinesefirstchar (' I am the author ') first character all letters + lowercase
* return "Wo"
*
* For example: Getchinesefirstchar (' I am author ', true) first Character first letter + lowercase
* return "W"
*
* Example: Getchinesefirstchar (' I am author ', true,true) first letter + uppercase
* return "W"
*
* For example: Getchinesefirstchar (' I am author ', false,true) first character all letters + uppercase
* return "WO"
*/
static public Function Getchinesefirstchar ($string, $isOne =false, $upper =false) {
$spellArray = Self::getarray ();
$str _arr = Self::utf8_str_split ($string, 1); Splitting a string into an array

if (Preg_match ('/^[\x{4e00}-\x{9fa5}]+$/u ', $str _arr[0])) {//Determine if it is a kanji
$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 Convert a string into a phonetic string
* @param $string Kanji string
* @param $upper Uppercase or lowercase
* @return String
*
* For example: Getchinesechar (' I am the author '); All strings + lowercase
* return "wo shi Zuo zhe"
*
* For example: Getchinesechar (' I am the author ', true); First Letter + lowercase
* return "W s z z"
*
* For example: Getchinesechar (' I am the author ', true,true); First Letter + Uppercase
* return "W S z z"
*
* For example: Getchinesechar (' I am the author ', false,true); First Letter + Uppercase
* return "WO SHI ZUO ZHE"
*/
static public Function Getchinesechar ($string, $isOne =false, $upper =false) {
Global $spellArray;
$str _arr = Self::utf8_str_split ($string, 1); Splitting 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 convert strings to an array
* @param $str the array to convert
* @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];
}
}

http://www.bkjia.com/PHPjc/736844.html www.bkjia.com true http://www.bkjia.com/PHPjc/736844.html techarticle Copy the code as follows:? PHP class helper_spell{Public $spellArray = Array (), static public function GetArray () {return Unseriali Ze (file_get_contents (' Pytable_without_tune.txt ...

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