php?overtrue/pinyin拓展實現(xiàn)漢字轉(zhuǎn)拼音
一:安裝overtrue/pinyin拓展
composer require overtrue/pinyin
overtrue/pinyin拓展GitHub地址:https://github.com/overtrue/p...
二:overtrue/pinyin拓展簡單使用
php漢字轉(zhuǎn)拼音我使用到了overtrue/pinyin拓展
overtrue/pinyin拼音轉(zhuǎn)成拼音常見的轉(zhuǎn)化方式:
PINYIN_TONE #帶音調(diào)的轉(zhuǎn)化方式 PINYIN_KEEP_NUMBER #保留數(shù)字的轉(zhuǎn)化方式 PINYIN_KEEP_ENGLISH#保留英文的轉(zhuǎn)化方式 PINYIN_KEEP_PUNCTUATION#保留標點的轉(zhuǎn)化方式 PINYIN_UMLAUT_V#使用 v 代替 yu的轉(zhuǎn)化方式
1:漢字轉(zhuǎn)成拼音數(shù)組
(1):漢字轉(zhuǎn)成無無音調(diào)的拼音
$pinyin = new Pinyin(); $pinyin->convert('測試漢字轉(zhuǎn)拼音');
輸出數(shù)據(jù)為:
['ce', 'shi', 'han', 'zi', 'zhuan', 'pin', 'yin']
(2):漢字轉(zhuǎn)成帶音調(diào)的拼音
$pinyin = new Pinyin(); $pinyin->convert('測試漢字轉(zhuǎn)拼音', PINYIN_TONE);
輸出數(shù)據(jù)為:
['cè', 'shì', 'hàn', 'zì', 'zhuǎn', 'pīn', 'yīn']
(3):漢字轉(zhuǎn)拼音中l(wèi)v拼音為lyu,使用此方法使用 v 代替 yu
$pinyin = new Pinyin(); $pinyin->convert('呂氏春秋'); $pinyin->convert('呂氏春秋', PINYIN_UMLAUT_V);
第一個方法輸出為:
['lyu', 'shi', 'chun', 'qiu']
第二個方法輸出為:
['lv', 'shi', 'chun', 'qiu']
2:漢字轉(zhuǎn)成拼音字符串
$pinyin = new Pinyin(); $pinyin->permalink('測試漢字轉(zhuǎn)拼音'); $pinyin->permalink('測試漢字轉(zhuǎn)拼音', '.');
第一個方法輸出為:
ce-shi-han-zi-zhuan-pin-yin
第二個方法輸出為:
ce.shi.han.zi.zhuan.pin.yi
3:漢字轉(zhuǎn)成首字母字符串
$pinyin = new Pinyin(); $pinyin->abbr('測試漢字轉(zhuǎn)拼音'); $pinyin->abbr('測試漢字轉(zhuǎn)拼音', '-');
第一個方法輸出為:
cshzzpy
第二個方法輸出為:
c-s-h-z-z-p-y
4:整段漢字(帶標點符號)轉(zhuǎn)成拼音
$pinyin = new Pinyin(); $pinyin->sentence('測試漢字轉(zhuǎn)拼音,這是測試!!!'); $pinyin->sentence('測試漢字轉(zhuǎn)拼音,這是測試!!!', '-');
第一個方法輸出為:
ce shi han zi zhuan pin yin, zhe shi ce shi!!!
第二個方法輸出為:
ce-shi-han-zi-zhuan-pin-yin,-zhe-shi-ce-shi!!!
5:姓名漢字轉(zhuǎn)拼音
有些漢字常見讀音和作為姓名讀音不同,例如仇,常見讀音為chou,但是作為姓氏時都qiu
$pinyin = new Pinyin(); $pinyin->name('仇某某');
輸出結(jié)果為:
['qiu', 'mou', 'mou']
以上就是php overtrue/pinyin拓展實現(xiàn)漢字轉(zhuǎn)拼音的詳細內(nèi)容,更多關(guān)于php overtrue pinyin漢字轉(zhuǎn)拼音的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
學習php設(shè)計模式 php實現(xiàn)觀察者模式(Observer)
這篇文章主要介紹了php設(shè)計模式中的觀察者模式,使用php實現(xiàn)觀察者模式,感興趣的小伙伴們可以參考一下2015-12-12