基于CC-CEDICT词典的中文转拼音工具,更准确的支持多音字的汉字转拼音解决方案。
安装使用Composer安装:
composer require "overtrue/pinyin:~3.0"使用拼音数组use Overtrue\Pinyin\Pinyin;$pinyin = new Pinyin();$pinyin->convert('带着希望去旅行,比到达终点更美好');// ["dai", "zhe", "xi", "wang", "qu", "lu", "xing", "bi", "dao", "da", "zhong", "dian", "geng", "mei", "hao"]$pinyin->convert('带着希望去旅行,比到达终点更美好', PINYIN_UNICODE);// ["dài","zhe","xī","wàng","qù","lǚ","xíng","bǐ","dào","dá","zhōng","diǎn","gèng","měi","hǎo"]$pinyin->convert('带着希望去旅行,比到达终点更美好', PINYIN_ASCII);//["dai4","zhe","xi1","wang4","qu4","lv3","xing2","bi3","dao4","da2","zhong1","dian3","geng4","mei3","hao3"]选项:
选项描述PINYIN_NONE不带音调输出:meihaoPINYIN_ASCII带数字式音调:mei3hao3PINYIN_UNICODEUNICODE式音调:měihǎo生成用于链接的拼音字符串$pinyin->permlink('带着希望去旅行'); // dai-zhe-xi-wang-qu-lu-xing$pinyin->permlink('带着希望去旅行', '.'); // dai.zhe.xi.wang.qu.lu.xing获取首字符字符串$pinyin->abbr('带着希望去旅行'); // dzxwqlx$pinyin->abbr('带着希望去旅行', '-'); // d-z-x-w-q-l-x翻译整段文字为拼音将会保留中文字符:,。!?:“”‘’并替换为对应的英文符号。
$pinyin->sentence('带着希望去旅行,比到达终点更美好!');// dai zhe xi wang qu lv xing, bi dao da zhong dian geng mei hao!$pinyin->sentence('带着希望去旅行,比到达终点更美好!', true);// dài zhe xī wàng qù lǚ xíng, bǐ dào dá zhōng diǎn gèng měi hǎo!翻译姓名
姓名的姓的读音有些与普通字不一样,比如‘单’常见的音为dan,而作为姓的时候读shan。
$pinyin->name('单某某'); // ['shan', 'mou', 'mou']$pinyin->name('单某某', PINYIN_UNICODE); // ["shàn","mǒu","mǒu"]在Laravel中使用独立的包在这里:overtrue/laravel-pinyin
Contribution欢迎提意见及完善补充词库tools/patches/
评论