PHP 中文工具類,支援漢字轉拼音、拼音分詞、簡繁互轉。
PHP Chinese Tool class, support Chinese pinyin, pinyin participle, simplified and traditional conversion
目前本類別庫擁有的三個功能,都是在實際開發過程中整理出來的。這次使用的資料不同於以前我開源過漢字轉拼音和簡繁互轉,資料都是從字典網站收集下來的,比以前的資料更準確。
由於中文的博大精深,字有多音字,簡體字和繁體字也有多種對應。且本類庫傳回的所有結果,均為包含所有組合的陣列。
本類庫字典資料載入後會佔用40+ MB 內存,在訪問量大的介面要使用此類漢字轉拼音、繁簡轉換功能時,推薦用Swoole 開發一個非同步服務程序,只需載入一次數據,就可以持續有效率地為你提供服務。
使用說明
Composer 直接安裝
composer require yurunsoft/chinese-util
Composer 項目配置引入
"require": { "yurunsoft/chinese-util" : "~1.0" }
功能
#漢字轉拼音
##
use \Yurun\Util\Chinese; $string = '恭喜發財!把我翻译成拼音看下?'; echo $string, PHP_EOL; echo '所有结果:', 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 '读音数字:', 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, '/'));
/** 输出结果: 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à " } } 读音数字: 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, '"的分词结果:', PHP_EOL; var_dump(Chinese::splitPinyin($string2)); /** 输出结果: "xianggang"的分词结果: array(2) { [0]=> string(12) "xi ang gang " [1]=> string(11) "xiang gang " } * /簡繁互轉
use \Yurun\Util\Chinese; $string3 = '中华人民共和国!恭喜發財!'; echo '"', $string3, '"的简体转换:', PHP_EOL; var_dump(Chinese::toSimplified($string3)); echo '"', $string3, '"的繁体转换:', PHP_EOL; var_dump(Chinese::toTraditional($string3)); /** 输出结果: "中华人民共和国!恭喜發財!"的简体转换: array(1) { [0]=> string(39) "中华人民共和国!恭喜发财!" } "中华人民共和国!恭喜發財!"的繁体转换: array(1) { [0]=> string(39) "中華人民共和國!恭喜發財!" } * /相關推薦:
以上是漢轉拼與拼音分詞的PHP中文工具類的詳細內容。更多資訊請關注PHP中文網其他相關文章!