PHP Chinese to pinyin function
Today, someone in Phpcms asked a question, that is, how to convert the Chinese into pinyin, this problem I was the first time to see, so Baidu a bit, found a usable function
{$_q = Ord (substr ($_string, + + $i, 1)), $_p = $_p*256 + $_q-65536; } $_res. = _pinyin ($_p, $_data); } return Preg_replace ("/[^a-z0-9]*/", "', $_res); } function _pinyin ($_num, $_data) {if ($_num>0 && $_num<160) {retur n Chr ($_num); }elseif ($_num<-20319 | | $_num>-10247) {return '; }else{foreach ($_data as $k = $v) {if ($v <=$_num) break;} return $k; }} function _u2_utf8_gb ($_c) {$_string = '; if ($_c < 0x80) {$_string. = $_c; }elseif ($_c < 0x800) {$_string. = Chr (0xC0 | $_c>>6); $_string. = Chr (0x80 | $_c & 0x3F); }elseif ($_c < 0x10000) { $_string. = Chr (0xE0 | $_c>>12); $_string. = Chr (0x80 | $_c>>6 & 0x3F); $_string. = Chr (0x80 | $_c & 0x3F); }elseif ($_c < 0x200000) {$_string. = Chr (0xF0 | $_c>>18); $_string. = Chr (0x80 | $_c>>12 & 0x3F); $_string. = Chr (0x80 | $_c>>6 & 0x3F); $_string. = Chr (0x80 | $_c & 0x3F); } return Iconv (' UTF-8 ', ' GB2312 ', $_string); }//Test echo Pinyin (' Chinese text ', ' gb2312 '); The second parameter "1" can be arbitrarily set as UTF8 encoding?>
the output is: Zhongwenzi