PHP implementation of Chinese character to Pinyin

Time:2019-11-8

I used Overtrue / Pinyin to expand

One: install Overtrue / Pingyin expansion

composer require overtrue/pinyin

Overtrue / Pingyin extended GitHub address: https://github.com/overtrue/p

Two: Overtrue / Pingyin is extended and easy to use

Overtrue / Pinyin common conversion methods for Pinyin Conversion:

Pinyin ﹐ tone ﹐ conversion mode with tone
Pinyin? Keep? Number? Conversion method of reserved numbers
Pinyin ﹐ keep ﹐ English ﹐ keep the way of English translation
Pinyin ﹐ keep ﹐ punctuation ﹐ conversion method of retaining punctuation
Pinyin? Umlaut? V? Conversion method using V instead of Yu

1: convert Chinese characters to Pinyin array

(1): conversion of Chinese characters into pinyin without tone

$pinyin = new Pinyin();
$Pinyin - > convert ('test Chinese character to pinyin ');

The output data is:

['ce', 'shi', 'han', 'zi', 'zhuan', 'pin', 'yin']

(2): conversion of Chinese characters into pinyin with tone

$pinyin = new Pinyin();
$Pinyin - > convert ('test Chinese character to Pinyin, pinyin_tone);

The output data is:

['cè', 'shì', 'hàn', 'zì', 'zhuǎn', 'pīn', 'yīn']

(3): in the conversion of Chinese characters to Pinyin, LV pinyin is Lyu. Use this method to use v instead of Yu

$pinyin = new Pinyin();
$Pingyin - > convert ('lu spring and Autumn ');
$Pingyin - > convert ('lu spring and Autumn Period ', Pinyin ﹤ umlaut ﹤ V);

The first method output is:

['lyu', 'shi', 'chun', 'qiu']

The second method output is:

['lv', 'shi', 'chun', 'qiu']

2: convert Chinese characters to Pinyin strings

$pinyin = new Pinyin();
$Pinyin - > permalink ('test Chinese character to pinyin ');
$Pinyin - > permalink ('test Chinese character to pinyin ','. ');

The first method output is:

ce-shi-han-zi-zhuan-pin-yin

The second method output is:

ce.shi.han.zi.zhuan.pin.yi

3: convert Chinese character to initial string

$pinyin = new Pinyin();
$Pinyin - > abbr ('test Chinese character to pinyin ');
$Pinyin - > abbr ('test Chinese character to pinyin', '-');

The first method output is:

cshzzpy

The second method output is:

c-s-h-z-z-p-y

4: convert the whole Chinese character (with punctuation) into pinyin

$pinyin = new Pinyin();
$Pingyin - > sentence ('test the conversion of Chinese characters to Pinyin, this is the test!!! ');
$Pingyin - > sentence ('test Chinese character to Pinyin, this is test!!! ',' - ');

The first method output is:

ce shi han zi zhuan pin yin, zhe shi ce shi!!!

The second method output is:

ce-shi-han-zi-zhuan-pin-yin,-zhe-shi-ce-shi!!!

5: the pronunciation of some Chinese characters is different from the pronunciation of names. For example, Qiu, the pronunciation of some Chinese characters is Chou, but all of them are Qiu as surnames

$pinyin = new Pinyin();
$Pingyin - > name ('qiu... ');

The output result is:

['qiu', 'mou', 'mou']