Home >Backend Development >PHP Problem >How to convert Chinese to Pinyin in php

How to convert Chinese to Pinyin in php

silencement
silencementOriginal
2019-12-23 09:38:5113277browse

How to convert Chinese to Pinyin in php

I used overtrue/pinyin extension in php to convert Chinese characters to pinyin

Install overtrue/pinyin extension

Support composer installation

composer require overtrue/pinyin

overtrue/pinyin Expansion Simple use

overtrue/pinyin Common conversion methods from pinyin to pinyin:

PINYIN_TONE   #带音调的转化方式
PINYIN_KEEP_NUMBER  #保留数字的转化方式
PINYIN_KEEP_ENGLISH#保留英文的转化方式
PINYIN_KEEP_PUNCTUATION#保留标点的转化方式
PINYIN_UMLAUT_V#使用 v 代替 yu的转化方式

1: Convert Chinese characters into pinyin array

(1 ): Convert Chinese characters to Pinyin without tone

$pinyin = new Pinyin();

$pinyin->convert('Test Chinese characters to Pinyin');

The output data is:

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

(2): Convert Chinese characters into pinyin with tones

$pinyin = new Pinyin();

$pinyin-> convert('Test Chinese characters to Pinyin', PINYIN_TONE);

The output data is:

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

(3): The pinyin of lv in Chinese pinyin is lyu, use this method to use v instead of yu

$pinyin = new Pinyin();

$pinyin->convert('Lu's Spring and Autumn Period');

$pinyin->convert('Lu's Spring and Autumn Period', PINYIN_UMLAUT_V) ;

The output of the first method is:

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

The second method The output is:

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

2: Convert Chinese characters into Pinyin string

$pinyin = new Pinyin();

$pinyin->permalink('Test Chinese characters to Pinyin');

$pinyin-> ;permalink('Test Chinese characters to Pinyin', '.');

The output of the first method is:

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

The output of the second method is:

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

3: Convert Chinese characters into initial letter string

$pinyin = new Pinyin();

$pinyin->abbr('Test Chinese characters to Pinyin');

$pinyin->abbr('Test Chinese characters to pinyin', '-');

The output of the first method is:

cshzzpy

The output of the second method is:

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

The above is the detailed content of How to convert Chinese to Pinyin in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn