Examples of Chinese pinyin for PHP

Source: Internet
Author: User
Tags lowercase php class split

  This article mainly introduces the php Chinese pinyin of the example, the need for friends can refer to the following

  Code as follows: <?php class helper_spell{    Public $spellArray = Array ()       static public F Unction GetArray () {        return unserialize (file_get_contents (' pytable_without_tune.txt ')); nbsp      /**      * @desc get the first letter of the string      * @param $string the string to be converted   &NBSP ;  * @param $isOne Take the first letter      * @param $upper convert to uppercase      * @return string   &nbsp ;  *       * For example: Getchinesefirstchar (' I am the author ') first character all letters + lowercase      * return "wo"   &NB Sp  *       * 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 + capital      * Retu RN "W"      *       * For example: Getchinesefirstchar (' I am author ', false,true) first character all letters + capital   &NBS P  * return"WO"      */    static public function Getchinesefirstchar ($string, $isOne =false, $upper =false) {        $spellArray = Self::getarray ();         $STR _arr = Self::utf8_str_split ( $string, 1); Split the string into an array           if (Preg_match ('/^[x{4e00}-x{9fa5}]+$/u ', $str _arr[0])) {//Determine if it 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 convert string to phonetic string      * @param $string kanji characters Character string      * @param $upper uppercase      * @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); Initials + lowercase      * return "W s z z"      *       * For example: Getchinesechar (' I am the author ', T Rue,true); First letter + Capital      * return "W S z z"      *       * For example: Getchinesechar (' I am the author ', F Alse,true); First letter + Capital      * return "WO SHI ZUO ZHE"      */    static public function Getchines Echar ($string, $isOne =false, $upper =false) {        global $spellArray;         $ST R_arr = Self::utf8_str_split ($string, 1); Splits the string into an array         $result = array ();         foreach ($str _arr as $char)         {          &N Bsp if (Preg_match ('/^[x{4e00}-x{9fa5}]+$/u ', $char))             {                $chinese = $spellArray [$char];                 $chinese  = $chinese [0];            }else{                $chinese = $char; nbsp           {            $chinese = $isOne? SUBSTR ($chinese, 0,1): $chinese;             $result [] = $upper? Strtoupper ($chinese): $chinese;        }         return implode (', $result);    }    /**      * @desc Convert a string to a group of      * @param $str the array to be converted   &N Bsp  * @param $split _len      * @return array      */    Private Function utf8_s Tr_split ($str, $split _len=1) {          if (!preg_matCH ('/^[0-9]+$/', $split _len) | | $split _len < 1) {            return FALSE;        ,     & nbsp     $len = Mb_strlen ($str, ' UTF-8 ');           if ($len <= $split _len) {            return Array ($st R);        }         Preg_match_all ('/.{'). $split _len. '}| [^x00] {1, '. $split _len. '} $/us ', $str, $ar);           return $ar [0];    }}  
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.