PHP 中文工具類,支持漢字轉(zhuǎn)拼音、拼音分詞、簡繁互轉(zhuǎn)。
PHP Chinese Tool class, support Chinese pinyin, pinyin participle, simplified and traditional conversion
目前本類庫擁有的三個功能,都是在實際開發(fā)過程中整理出來的。這次使用的數(shù)據(jù)不同于以前我開源過漢字轉(zhuǎn)拼音和簡繁互轉(zhuǎn),數(shù)據(jù)都是從字典網(wǎng)站采集下來的,比以前的數(shù)據(jù)更加準(zhǔn)確。
由于中文的博大精深,字有多音字,簡體字和繁體字也有多種對應(yīng)。并且本類庫返回的所有結(jié)果,均為包含所有組合的數(shù)組。
本類庫字典數(shù)據(jù)加載后會占用 40+ MB 內(nèi)存,在訪問量大的接口要使用此類漢字轉(zhuǎn)拼音、繁簡轉(zhuǎn)換功能時,推薦用 Swoole 開發(fā)一個異步服務(wù)程序,只需加載一次數(shù)據(jù),就可以持續(xù)高效地為你提供服務(wù)。
使用說明
Composer 直接安裝
composer require yurunsoft/chinese-util
Composer 項目配置引入
"require": {
"yurunsoft/chinese-util" : "~1.0"
}
功能
漢字轉(zhuǎn)拼音
use \Yurun\Util\Chinese;
$string = '恭喜發(fā)財!把我翻譯成拼音看下?';
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 "
}
* /
簡繁互轉(zhuǎn)
use \Yurun\Util\Chinese;
$string3 = '中華人民共和國!恭喜發(fā)財!';
echo '"', $string3, '"的簡體轉(zhuǎn)換:', PHP_EOL;
var_dump(Chinese::toSimplified($string3));
echo '"', $string3, '"的繁體轉(zhuǎn)換:', PHP_EOL;
var_dump(Chinese::toTraditional($string3));
/**
輸出結(jié)果:
"中華人民共和國!恭喜發(fā)財!"的簡體轉(zhuǎn)換:
array(1) {
[0]=>
string(39) "中華人民共和國!恭喜發(fā)財!"
}
"中華人民共和國!恭喜發(fā)財!"的繁體轉(zhuǎn)換:
array(1) {
[0]=>
string(39) "中華人民共和國!恭喜發(fā)財!"
}
* /
您可能感興趣的文章:- PHP中文分詞 自動獲取關(guān)鍵詞介紹
- 開源php中文分詞系統(tǒng)SCWS安裝和使用實例
- PHP中文分詞的簡單實現(xiàn)代碼分享
- PHPAnalysis中文分詞類詳解
- php實現(xiàn)的中文分詞類完整實例
- php實現(xiàn)scws中文分詞搜索的方法
- 使用Discuz關(guān)鍵詞服務(wù)器實現(xiàn)PHP中文分詞
- php+正則將字符串中的字母數(shù)字和中文分割
- php中文語義分析實現(xiàn)方法示例