Examples of _php in php Chinese pinyin techniques

Source: Internet
Author: User
Tags lowercase

Copy Code code 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 $isOne whether to take the first letter
* @param $upper is converted to uppercase
* @return String
*
* For 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"
*
* For example: Getchinesefirstchar (' I am author ', true,true) first Character first letter + upper case
* return "W"
*
* For example: Getchinesefirstchar (' I am author ', false,true) the 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])) {//Judge whether it is Chinese characters
$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 character 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 a string 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];
}
}

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.