PHP Chinese Pinyin Class (UTF-8)
Demo
Demo text: People's Republic of China
You can implement any of the following three conversions
① into Hanyu Pinyin with a tonal tone
Zhōng Huárén Mín Gòng Héguó
② into Hanyu Pinyin with no tone
Zhong Hua ren min gong he guo
③ into the first letter of Hanyu Pinyin
Zhrmghg
If you have any questions, you can leave a message.
Example:
Note: The following classes require ChineseCharacters.dat files.
Full package with demo package can be downloaded here: http://download.csdn.net/detail/kin1987/7064125
<?php/** * * @Author: Kin * @Date: 2014-03-16 * */class chinesepinyin{//utf-8 Chinese Chinese character Collection private $Chinese
Characters;
Code Private $charset = ' utf-8 '; Public Function __construct () {if (Empty ($this->chinesecharacters)) {$this->chinesecharacters = File_get_cont
Ents (' ChineseCharacters.dat '); The * * * param to a tonal Hanyu Pinyin * param $input _char string needs to be converted to the kanji * $delimiter the delimiter between pinyin * param after a string conversion $outside
_ignore Boolean ignores non-kanji content/Public Function Transformwithtone ($input _char, $delimiter = ', $outside _ignore=false) {
$input _len = Mb_strlen ($input _char, $this->charset);
$output _char = ';
For ($i =0 $i < $input _len $i + +) {$word = Mb_substr ($input _char, $i, 1, $this->charset); if (Preg_match ('/^[\x{4e00}-\x{9fa5}]$/u ', $word) && preg_match ('/\, '. Preg_quote ($word). " (.*?)
\,/', $this->chinesecharacters, $matches)) {$output _char.= $matches [1]. $delimiter;
}else if (! $outside _ignore) {$output _char.= $word; }
}
return $output _char; * * * Convert to with no tone of Hanyu Pinyin * param $input _char string requires conversion of Kanji * param $delimiter String after the conversion between phonetic separator * param $outside _ig
Nore Boolean ignores non-kanji content/Public Function Transformwithouttone ($input _char, $delimiter = ', $outside _ignore=true) {
$char _with_tone = $this->transformwithtone ($input _char, $delimiter, $outside _ignore); $char _without_tone = str_replace (Array (' Do ', ' á ', ' ǎ ', ' à '), ' ō ', ' æ ', ' ǒ ', ' driving ', ' ē ', ' é ', ' ě ', ' è ', ' ī ', ' í ', ' ǐ ', ' ì ', ' were thinking ', ' ú ', ' Ǔ ', ' Ù ', ' Ǖ ', ' Ǘ ', ' ǚ ', ' Ǜ ', ' u '), array (' A ', ' a ', ' a ', ' a ', ' a ', ' a ', ' o ', ' o ', ' o ', ' o ', ' e ', ' e ', ' e ', ' e ', ' I ', ' I ', ' I ', ' I ', ' u ', ' u ', '
', ' u ', ' V ', ' V ', ' V ', ' V ', ' V ', $char _with_tone);
return $char _without_tone; * * Convert to Hanyu Pinyin * param $input _char string needs to convert Kanji * param after string conversion the separator/public function between pinyin Transformucwords ($input _char, $delimiter = ') {$char _without_tone = ucwords ($this->transformwithouttone ($input _
Char, ', true '); $ucwords = preg_replace ('/[^a-z]/', ', ', $char _wiThout_tone);
if (!empty ($delimiter)) {$ucwords = implode ($delimiter, Str_split ($ucwords));
return $ucwords; }
}
Note: The above class requires ChineseCharacters.dat files.
Full package with demo package can be downloaded here: http://download.csdn.net/detail/kin1987/7064125