A Chinese to Pinyin conversion tool based on the mozillazg/pinyin-data dictionary, a more accurate solution for converting Chinese characters to Pinyin that supports multi-phonetic characters.
Like my project? Click here to support me
Install using Composer:
composer require overtrue/pinyin:^5.0
Except for the method of getting the first letter, all methods support a second parameter, which is used to specify the pinyin format. The optional values are:
symbol
(default) tone symbol, such as pīn yīn
none
does not output pinyin, such as pin yin
number
, such as pin1 yin1
Except for permalink
which returns a string, all other methods return the collection type OvertruePinyinCollection
:
use Overtrue Pinyin Pinyin ;
$ pinyin = Pinyin:: sentence ( '你好,世界' );
You can access collection content via:
echo $ pinyin ; // nǐ hǎo shì jiè
// 直接将对象转成字符串
$ string = ( string ) $ pinyin ; // nǐ hǎo shì jiè
$ pinyin -> toArray (); // ['nǐ', 'hǎo', 'shì', 'jiè']
// 直接使用索引访问
$ pinyin [ 0 ]; // 'nǐ'
// 使用函数遍历
$ pinyin -> map ( ' ucfirst ' ); // ['Nǐ', 'Hǎo', 'Shì', 'Jiè']
// 拼接为字符串
$ pinyin -> join ( ' ' ); // 'nǐ hǎo shì jiè'
$ pinyin -> join ( ' - ' ); // 'nǐ-hǎo-shì-jiè'
// 转成 json
$ pinyin -> toJson (); // '["nǐ","hǎo","shì","jiè"]'
json_encode ( $ pinyin ); // '["nǐ","hǎo","shì","jiè"]'
use Overtrue Pinyin Pinyin ;
echo Pinyin:: sentence ( '带着希望去旅行,比到达终点更美好' );
// dài zhe xī wàng qù lǚ xíng , bǐ dào dá zhōng diǎn gèng měi hǎo
// 去除声调
echo Pinyin:: sentence ( '带着希望去旅行,比到达终点更美好' , ' none ' );
// dai zhe xi wang qu lv xing , bi dao da zhong dian geng mei hao
// 保留所有非汉字字符
echo Pinyin:: fullSentence ( 'ル是片假名,π是希腊字母' , ' none ' );
// ル shi pian jia ming ,π shi xi la zi mu
Usually used for article links, etc., you can use the permalink
method to obtain the pinyin string:
echo Pinyin:: permalink ( '带着希望去旅行' ); // dai-zhe-xi-wang-qu-lyu-xing
echo Pinyin:: permalink ( '带着希望去旅行' , ' . ' ); // dai.zhe.xi.wang.qu.lyu.xing
Usually used to create indexes for search, you can use the abbr
method to convert:
Pinyin:: abbr ( '带着希望去旅行' ); // ['d', 'z', 'x', 'w', 'q', 'l', 'x']
echo Pinyin:: abbr ( '带着希望去旅行' )-> join ( ' - ' ); // d-z-x-w-q-l-x
echo Pinyin:: abbr ( '你好2018! ' )-> join ( '' ); // nh2018
echo Pinyin:: abbr ( ' Happy New Year! 2018! ' )-> join ( '' ); // HNY2018
// 保留原字符串的英文单词
echo Pinyin:: abbr ( ' CGV电影院' , false , true )-> join ( '' ); // CGVdyy
initials
Convert the first letter as a surname and the rest as regular words:
Pinyin:: nameAbbr ( '欧阳' ); // ['o', 'y']
echo Pinyin:: nameAbbr ( '单单单' )-> join ( ' - ' ); // s-d-d
The pronunciation of the surname of a name is somewhat different from that of ordinary characters. For example, the common pronunciation of 'dan' is dan
, but when used as a surname, it is pronounced shan
.
Pinyin:: name ( '单某某' ); // ['shàn', 'mǒu', 'mǒu']
Pinyin:: name ( '单某某' , ' none ' ); // ['shan', 'mou', 'mou']
Pinyin:: name ( '单某某' , ' none ' )-> join ( ' - ' ); // shan-mou-mou
According to the national regulations regarding the reminder that the pinyin ü (Lu, Lu, Lu, Lu, Nv, etc.) of the name on the Chinese passport travel document is uniformly spelled as YU, convert ü
to yu
:
Pinyin:: passportName ( '吕小布' ); // ['lyu', 'xiao', 'bu']
Pinyin:: passportName ( '女小花' ); // ['nyu', 'xiao', 'hua']
Pinyin:: passportName ( '律师' ); // ['lyu', 'shi']
The return value of polyphonetic characters is a collection of associative arrays. By default, all pronunciations after deduplication are returned:
$ pinyin = Pinyin:: polyphones ( '重庆' );
$ pinyin [ '重' ]; // ["zhòng", "chóng", "tóng"]
$ pinyin [ '庆' ]; // ["qìng"]
$ pinyin -> toArray ();
// [
// "重": ["zhòng", "chóng", "tóng"],
// "庆": ["qìng"]
// ]
If you don’t want to remove duplicates, you can return it in array form:
$ pinyin = Pinyin:: polyphones ( '重庆重庆' , Converter:: TONE_STYLE_SYMBOL , true );
// or
$ pinyin = Pinyin:: polyphonesAsArray ( '重庆重庆' , Converter:: TONE_STYLE_SYMBOL );
$ pinyin -> toArray ();
// [
// ["重" => ["zhòng", "chóng", "tóng"]],
// ["庆" => ["qìng"]],
// ["重" => ["zhòng", "chóng", "tóng"]],
// ["庆" => ["qìng"]]
// ]
Similar to polyphonic characters, the return value of a single character is a string. Polyphonic characters will be adjusted according to the frequency of the character to obtain common sounds:
$ pinyin = Pinyin:: chars ( '重庆' );
echo $ pinyin [ '重' ]; // "zhòng"
echo $ pinyin [ '庆' ]; // "qìng"
$ pinyin -> toArray ();
// [
// "重": "zhòng",
// "庆": "qìng"
// ]
Warning
When processing single words, because polyphonic words obtain common sounds from the word frequency table, incorrect situations may occur in word environments. It is recommended to use polyphonic word processing.
Please refer to the test cases for more usage.
According to the regulations of the National Language and Character Working Committee, lv
, lyu
, and lǚ
are all correct, but lv
is the most commonly used, so lv
is used by default. If you need to use other ones, you can pass them in during initialization:
echo Pinyin:: sentence ( '旅行' );
// lǚ xíng
echo Pinyin:: sentence ( '旅行' , ' none ' );
// lv xing
echo Pinyin:: yuToYu ()-> sentence ( '旅行' , ' none ' );
// lyu xing
echo Pinyin:: yuToU ()-> sentence ( '旅行' , ' none ' );
// lu xing
echo Pinyin:: yuToV ()-> sentence ( '旅行' , ' none ' );
// lv xing
Warning
Only valid when Pinyin style is non-
none
mode.
You can use the command line to convert Pinyin:
php ./bin/pinyin 带着希望去旅行 --method=sentence --tone-style=symbol
# dài zhe xī wàng qù lǚ xíng
For more usage methods, you can view the help documentation:
php ./bin/pinyin --help
# Usage:
# ./pinyin [chinese] [method] [options]
# Options:
# -j, --json 输出 JSON 格式.
# -c, --compact 不格式化输出 JSON.
# -m, --method=[method] 转换方式,可选:sentence/sentenceFull/permalink/abbr/nameAbbr/name/passportName/phrase/polyphones/chars.
# --no-tone 不使用音调.
# --tone-style=[style] 音调风格,可选值:symbol/none/number, default: none.
# -h, --help 显示帮助.
The independent package is here: overtrue/laravel-pinyin
If you have this need, you can also learn about my other package: overtrue/php-opencc
Welcome to provide comments and improve the vocabulary:
If you like my project and want to support it, click here
Many thanks to Jetbrains for kindly providing a license for me to work on this and other open-source projects.
Wondering how to build a PHP extension package from scratch?
Please pay attention to my practical course, where I will share some extension development experience - "PHP Extension Pack Practical Tutorial - From Getting Started to Release"
MIT