這篇文章將為大家詳細(xì)講解有關(guān)如何使用支持漢轉(zhuǎn)拼和拼音分詞的PHP中文工具類ChineseUtil,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
在天寧等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì) 網(wǎng)站設(shè)計(jì)制作定制網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站建設(shè),全網(wǎng)整合營(yíng)銷推廣,外貿(mào)網(wǎng)站制作,天寧網(wǎng)站建設(shè)費(fèi)用合理。PHP 中文工具類,支持漢字轉(zhuǎn)拼音、拼音分詞、簡(jiǎn)繁互轉(zhuǎn)。
PHP Chinese Tool class, support Chinese pinyin, pinyin participle, simplified and traditional conversion
目前本類庫(kù)擁有的三個(gè)功能,都是在實(shí)際開發(fā)過程中整理出來的。這次使用的數(shù)據(jù)不同于以前我開源過漢字轉(zhuǎn)拼音和簡(jiǎn)繁互轉(zhuǎn),數(shù)據(jù)都是從字典網(wǎng)站采集下來的,比以前的數(shù)據(jù)更加準(zhǔn)確。
由于中文的博大精深,字有多音字,簡(jiǎn)體字和繁體字也有多種對(duì)應(yīng)。并且本類庫(kù)返回的所有結(jié)果,均為包含所有組合的數(shù)組。
本類庫(kù)字典數(shù)據(jù)加載后會(huì)占用 40+ MB 內(nèi)存,在訪問量大的接口要使用此類漢字轉(zhuǎn)拼音、繁簡(jiǎn)轉(zhuǎn)換功能時(shí),推薦用 Swoole 開發(fā)一個(gè)異步服務(wù)程序,只需加載一次數(shù)據(jù),就可以持續(xù)高效地為你提供服務(wù)。
使用說明
Composer 直接安裝
composer require yurunsoft/chinese-util
Composer 項(xiàng)目配置引入
"require": { "yurunsoft/chinese-util" : "~1.0" }
功能
漢字轉(zhuǎn)拼音
use \Yurun\Util\Chinese; $string = '恭喜發(fā)財(cái)!把我翻譯成拼音看下?'; echo $string, PHP_EOL; echo '所有結(jié)果:', PHP_EOL; var_dump(Chinese::toPinyin($string)); echo '全拼:', PHP_EOL; var_dump(Chinese::toPinyin($string, Pinyin::CONVERT_MODE_PINYIN)); echo '首字母:', PHP_EOL; var_dump(Chinese::toPinyin($string, Pinyin::CONVERT_MODE_PINYIN_FIRST)); echo '讀音:', PHP_EOL; var_dump(Chinese::toPinyin($string, Pinyin::CONVERT_MODE_PINYIN_SOUND)); echo '讀音數(shù)字:', PHP_EOL; var_dump(Chinese::toPinyin($string, Pinyin::CONVERT_MODE_PINYIN_SOUND_NUMBER)); echo '自選 + 自定義分隔符:', PHP_EOL; var_dump(Chinese::toPinyin($string, Pinyin::CONVERT_MODE_PINYIN | Pinyin::CONVERT_MODE_PINYIN_SOUND_NUMBER, '/'));
/** 輸出結(jié)果: array(4) { ["pinyin"]=> array(1) { [0]=> string(58) "gong xi fa cai ! ba wo fan yi cheng pin yin kan xia ? " } ["pinyinSound"]=> array(4) { [0]=> string(63) "gōng xǐ fā cái bǎ wǒ fān yì chéng pīn yīn kàn xià " [1]=> string(63) "gōng xǐ fā cái bà wǒ fān yì chéng pīn yīn kàn xià " [2]=> string(63) "gōng xǐ fā cái bǎ wǒ fān yì chéng pīn yīn kān xià " [3]=> string(63) "gōng xǐ fā cái bà wǒ fān yì chéng pīn yīn kān xià " } ["pinyinSoundNumber"]=> array(4) { [0]=> string(63) "gong1 xi3 fa1 cai2 ba3 wo3 fan1 yi4 cheng2 pin1 yin1 kan4 xia4 " [1]=> string(63) "gong1 xi3 fa1 cai2 ba4 wo3 fan1 yi4 cheng2 pin1 yin1 kan4 xia4 " [2]=> string(63) "gong1 xi3 fa1 cai2 ba3 wo3 fan1 yi4 cheng2 pin1 yin1 kan1 xia4 " [3]=> string(63) "gong1 xi3 fa1 cai2 ba4 wo3 fan1 yi4 cheng2 pin1 yin1 kan1 xia4 " } ["pinyinFirst"]=> array(1) { [0]=> string(34) "g x f c ! b w f y c p y k x ? " } } 全拼: array(1) { ["pinyin"]=> array(1) { [0]=> string(58) "gong xi fa cai ! ba wo fan yi cheng pin yin kan xia ? " } } 首字母: array(1) { ["pinyinFirst"]=> array(1) { [0]=> string(34) "g x f c ! b w f y c p y k x ? " } } 讀音: array(1) { ["pinyinSound"]=> array(4) { [0]=> string(63) "gōng xǐ fā cái bǎ wǒ fān yì chéng pīn yīn kàn xià " [1]=> string(63) "gōng xǐ fā cái bà wǒ fān yì chéng pīn yīn kàn xià " [2]=> string(63) "gōng xǐ fā cái bǎ wǒ fān yì chéng pīn yīn kān xià " [3]=> string(63) "gōng xǐ fā cái bà wǒ fān yì chéng pīn yīn kān xià " } } 讀音數(shù)字: array(1) { ["pinyinSoundNumber"]=> array(4) { [0]=> string(63) "gong1 xi3 fa1 cai2 ba3 wo3 fan1 yi4 cheng2 pin1 yin1 kan4 xia4 " [1]=> string(63) "gong1 xi3 fa1 cai2 ba4 wo3 fan1 yi4 cheng2 pin1 yin1 kan4 xia4 " [2]=> string(63) "gong1 xi3 fa1 cai2 ba3 wo3 fan1 yi4 cheng2 pin1 yin1 kan1 xia4 " [3]=> string(63) "gong1 xi3 fa1 cai2 ba4 wo3 fan1 yi4 cheng2 pin1 yin1 kan1 xia4 " } } 自選 + 自定義分隔符: array(2) { ["pinyin"]=> array(1) { [0]=> string(58) "gong/xi/fa/cai/!/ba/wo/fan/yi/cheng/pin/yin/kan/xia/?/" } ["pinyinSoundNumber"]=> array(4) { [0]=> string(63) "gong1/xi3/fa1/cai2/ba3/wo3/fan1/yi4/cheng2/pin1/yin1/kan4/xia4/" [1]=> string(63) "gong1/xi3/fa1/cai2/ba4/wo3/fan1/yi4/cheng2/pin1/yin1/kan4/xia4/" [2]=> string(63) "gong1/xi3/fa1/cai2/ba3/wo3/fan1/yi4/cheng2/pin1/yin1/kan1/xia4/" [3]=> string(63) "gong1/xi3/fa1/cai2/ba4/wo3/fan1/yi4/cheng2/pin1/yin1/kan1/xia4/" } } * /
拼音分詞
use \Yurun\Util\Chinese; $string2 = 'xianggang'; echo '"', $string2, '"的分詞結(jié)果:', PHP_EOL; var_dump(Chinese::splitPinyin($string2)); /** 輸出結(jié)果: "xianggang"的分詞結(jié)果: array(2) { [0]=> string(12) "xi ang gang " [1]=> string(11) "xiang gang " } * /
簡(jiǎn)繁互轉(zhuǎn)
use \Yurun\Util\Chinese; $string3 = '中華人民共和國(guó)!恭喜發(fā)財(cái)!'; echo '"', $string3, '"的簡(jiǎn)體轉(zhuǎn)換:', PHP_EOL; var_dump(Chinese::toSimplified($string3)); echo '"', $string3, '"的繁體轉(zhuǎn)換:', PHP_EOL; var_dump(Chinese::toTraditional($string3)); /** 輸出結(jié)果: "中華人民共和國(guó)!恭喜發(fā)財(cái)!"的簡(jiǎn)體轉(zhuǎn)換: array(1) { [0]=> string(39) "中華人民共和國(guó)!恭喜發(fā)財(cái)!" } "中華人民共和國(guó)!恭喜發(fā)財(cái)!"的繁體轉(zhuǎn)換: array(1) { [0]=> string(39) "中華人民共和國(guó)!恭喜發(fā)財(cái)!" } * /
關(guān)于“如何使用支持漢轉(zhuǎn)拼和拼音分詞的PHP中文工具類ChineseUtil”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。