<?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];
}
}