PHP如何將中文轉為拼音?
首先使用Composer安裝「overtrue/pinyin」拓展;
composer require overtrue/pinyin
然後引入Pinyin類,併範例化該類;
use OvertruePinyinPinyin; $pinyin = new Pinyin();
接著呼叫範例化後Pinyin物件中「convert」方法,並將中文傳入該方法;
$pinyin->convert('測試漢字轉拼音');
最後接收轉後的拼音即可。
['ce', 'shi', 'han', 'zi', 'zhuan', 'pin', 'yin']
範例
use OvertruePinyinPinyin; // 小記憶體型 $pinyin = new Pinyin(); // 預設 // 記憶體型 // $pinyin = new Pinyin('OvertruePinyinMemoryFileDictLoader'); // I/O型 // $pinyin = new Pinyin('OvertruePinyinGeneratorFileDictLoader'); $pinyin->convert('帶著希望去旅行,比到達終點更美好'); // ["dai", "zhe", "xi", "wang", "qu", "lyu", "xing", "bi", "dao", "da", "zhong", "dian", "geng", "mei", "hao"] $pinyin->convert('帶著希望去旅行,比到達終點更美好', PINYIN_TONE); // ["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_TONE); //["dai4","zhe","xi1","wang4","qu4","lyu3","xing2","bi3","dao4","da2","zhong1","dian3","geng4","mei3","hao3"]
以上就是PHP如何將中文轉為拼音?的詳細內容,更多請關注TW511.COM其它相關文章!