PHP Kanji to Pinyin

Source: Internet
Author: User

Full Download:

Http://dwz.cn/6NnaP0

The core code is as follows:

<?php/**** Kanji **/class chinesepinyin{//utf-8 Chinese character set private $ChineseCharacters;//code private $charset = ' utf-8 '; Public Function __construct () {if (Empty ($this->chinesecharacters)) {$this->chinesecharacters = file_get_ Contents (' ChineseCharacters.dat ');}}      /** converted to phonetic pinyin * param $input _char string needs to be converted Kanji * param $delimiter string conversion between pinyin delimiter * param $outside _ignore Boolean Whether to ignore non-Chinese 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;} /** turn into a non-tonal Hanyu Pinyin * param $input _char string needs to be converted Kanji * param $delimiter string converted after the pinyin delimiter between * param $outside_ignore 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 (' ā ', ' á ', ' ǎ ', ' à ', ' ō ', ' Ó ', ' ǒ ', ' Ò ', ' ッ ', ' é ', ' canít ', ' è ', ' ī ', ' í ', ' ǐ ', ' ì ', ' ū ', ' ú ', ' ǔ ', ' Ù ', ' disables ', ' Ǘ ', ' clean ', ' Ǜ ', ' U '), array (' A ', ' a ', ' a ', ' a ', ' o ', ' o ', ' o ', ' o ', ' e ', ' e ', ' e ', ' e ', ' I ', ' I ', ' I ', ' I ', ' u ', ' u ', ' u ', ' u ', ' V ', ' V ', ' V ', ' V ', ' V ' ), $char _with_tone); return $char _without_tone;} /** converted to Hanyu Pinyin * param $input _char string needs to be converted Kanji * param $delimiter string converted between pinyin delimiter */public function 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;}}? >

Examples of Use:

<?phpinclude ' ChinesePinyin.class.php '; $Pinyin = new Chinesepinyin (); $words = ' kanji into pinyin class '; Echo ' 

  

;

PHP Kanji to Pinyin

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.