PHP Chinese Tool class Chineseutil how to convert Chinese characters and pinyin

Source: Internet
Author: User
This time to bring you the PHP Chinese tool class Chineseutil how to convert Chinese characters and pinyin, PHP Chinese tool class Chineseutil conversion of Chinese characters and pinyin note what, the following is the actual case, together to see.

PHP Chinese tools, support for Chinese characters to pinyin, pinyin word segmentation, simple and complex mutual turn.

PHP Chinese Tool class, support Chinese pinyin, pinyin participle, simplified and traditional conversion

At present, the library has three functions, are in the actual development process of sorting out. This use of data is different from the previous I open-source cross-Chinese pinyin and simple interactive transfer, the data are collected from the dictionary site, more accurate than the previous data.

Because of the profound Chinese language, the word has multi-tone characters, simplified and traditional characters also have a variety of correspondence. And all the results returned by this class library are arrays that contain all the combinations.

This kind of library dictionary data load will occupy 40+ MB of memory, in the large traffic interface to use this kind of kanji to pinyin, simplified conversion function, it is recommended to use Swoole to develop an asynchronous service program, just load the data once, you can continuously and efficiently provide services to you.

Instructions for use

Composer Direct Installation

composer require yurunsoft/chinese-util

Composer Project Configuration Introduction

"Require": {  "yurunsoft/chinese-util": "~1.0"}

Function

Kanji to Pinyin

Use \yurun\util\chinese; $string = ' Congratulations! Translate me into pinyin to see? '; Echo $string, Php_eol;echo ' All results: ', Php_eol;var_dump (Chinese::topinyin ($string)); Echo ' All spell: ', Php_eol;var_dump ( Chinese::topinyin ($string, Pinyin::convert_mode_pinyin)); Echo ' first letter: ', Php_eol;var_dump (Chinese::topinyin ($string, Pinyin::convert_mode_pinyin_first)); Echo ' pronunciation: ', php_eol;var_dump (Chinese::topinyin ($string, Pinyin::convert_mode_ Pinyin_sound)); Echo ' pronunciation number: ', Php_eol;var_dump (Chinese::topinyin ($string, Pinyin::convert_mode_pinyin_sound_number ); Echo ' optional + custom delimiter: ', Php_eol;var_dump (Chinese::topinyin ($string, Pinyin::convert_mode_pinyin | Pinyin::convert_mode_pinyin_sound_number, '/'));
/** output: Array (4) {["Pinyin"]=> Array (1) {[0]=> string] "Gong Xi fa cai!" ba wo fan yi cheng pin yin kan xia?  "} [" Pinyinsound "]=> Array (4) {[0]=> string (+)" Gōng xǐfācái bǎwǒfān yìchéng pīn yīn kàn xià "[1]=> String (gōng xǐfācái bàwǒfān yìchéng pīn yīn kàn xià "[2]=> string (+)" Gōng xǐfācái bǎwǒfān yìchéng p Īn yīn kān Xià "[3]=> string (+)" Gōng xǐfācái bàwǒfān yìchéng pīn yīn kān xià "} [" Pinyinsoundnumber "]=> A Rray (4) {[0]=> string (+) "Gong1 xi3 fa1 cai2 ba3 wo3 fan1 yi4 cheng2 pin1 yin1 kan4 xia4" [1]=> string (+) "G  Ong1 xi3 fa1 cai2 ba4 wo3 fan1 yi4 cheng2 pin1 yin1 kan4 xia4 "[2]=> string (+)" Gong1 xi3 fa1 cai2 ba3 wo3 fan1 yi4 Cheng2 pin1 yin1 kan1 xia4 "[3]=> string (+)" Gong1 xi3 fa1 cai2 ba4 wo3 fan1 yi4 cheng2 pin1 yin1 kan1 xia4 "} [" Pinyinfirst "]=> Array (1) {[0]=> string ()" G x f C! b w f y c p y k x? "}} Full spell: Array (1) {[" Pinyin "]=> Array (1) {[0]=> string("Gong Xi fa cai!") ba wo fan yi cheng pin yin kan xia? "}}" first letter: Array (1) {["Pinyinfirst"]=> Array (1) {[0]=> string () "G x f C! b w f y c p y k x?  "}} Pronunciations: Array (1) {[" Pinyinsound "]=> Array (4) {[0]=> string (+)" Gōng xǐfācái bǎwǒfān yìchéng pīn yīn kàn Xià "[1]=> string" Gōng xǐfācái bàwǒfān yìchéng pīn yīn kàn xià "[2]=> string (+)" Gōng xǐfācái bǎwǒ Fān yìchéng pīn yīn kān xià "[3]=> string (+)" Gōng xǐfācái bàwǒfān yìchéng pīn yīn kān Xià "}} Pronunciations: Array (1) {["Pinyinsoundnumber"]=> Array (4) {[0]=> string (x) "Gong1 xi3 fa1 cai2 ba3 wo3 fan1 yi4 cheng2 pin1 yin1 kan4 x  Ia4 "[1]=> string" gong1 xi3 fa1 cai2 ba4 wo3 fan1 yi4 cheng2 pin1 yin1 kan4 xia4 "[2]=> string (+)" gong1 Xi3 fa1 cai2 ba3 wo3 fan1 yi4 cheng2 pin1 yin1 kan1 xia4 "[3]=> string (+)" Gong1 xi3 fa1 cai2 ba4 wo3 fan1 yi4 che Ng2 pin1 yin1 kan1 xia4 "}}" optional + custom delimiter: Array (2) {["Pinyin"]=> Array (1) {[0]=> string (+) "gong/xi/fa/cai/! /ba/wo/fan/yi/cheng/pin/yin/kan/xia/? /"} [" Pinyinsoundnumber "]=> Array (4) {[0]=> string () gong1/xi3/fa1/cai2/ba3/wo3/fan1/yi4/cheng2/pin1/yin1/ kan4/xia4/"[1]=> string (+)" gong1/xi3/fa1/cai2/ba4/wo3/fan1/yi4/cheng2/pin1/yin1/kan4/xia4/"[2]=> string ( "gong1/xi3/fa1/cai2/ba3/wo3/fan1/yi4/cheng2/pin1/yin1/kan1/xia4/" [3]=> string "gong1/xi3/fa1/cai2/ba4/ wo3/fan1/yi4/cheng2/pin1/yin1/kan1/xia4/"}} */

Pinyin participle

Use \yurun\util\chinese; $string 2 = ' Xianggang '; Echo ' "', $string 2, '" participle result: ', Php_eol;var_dump (Chinese::splitpinyin ($ string2));/** output: "Xianggang" participle Result: Array (2) {[0]=> string (n) "Xi Ang Gang" [1]=> string (one) "Xiang Gang"} */

Simple and interactive transfer

Use \yurun\util\chinese; $string 3 = ' People's Republic of China! Congratulations, wealth! '; Echo ' ', $string 3, ' The Simplified conversion: ', Php_eol;var_dump (chinese::tosimplified ($string 3)); Echo ' "', $string 3, '" Traditional conversion: ', php_ Eol;var_dump (Chinese::totraditional ($string 3));/** output: "People's Republic of China!" Congratulations, wealth! "Simplified Conversion: Array (1) {[0]=> string (39)" People's Republic of China! Congratulations on getting rich! "}" The People's Republic of China! Congratulations, wealth! "Traditional Conversion: Array (1) {[0]=> string (39)" In the People's Republic of China! Congratulations, wealth! "} * /

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

PHP recursive function case use detailed

PHP implementation generated with two-dimensional code image and forced to download the steps in detail

Related Article

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.