各位条子大家好
本日给大家带来的是一款 笔墨处理类库:中文转拼音PHP类库!
汉字转拼音类(全拼与首字母)类,这个因此前在网上网络的一中文转拼音类,这个比较好用.现在分享给大家!
由于代码比较长,须要文件版的可以私聊我!
好了,废话不多说上源码!
<?php
/
Created by PhpStorm.
User: jifei
Date: 15/6/25
Time: 下午2:26
/
class Pinyin
{
//中笔墨符串
private static $string = '';
//拼音
private static $pinyin = '';
private static $encoding = 'UTF-8';
//短拼音
private static $short_pinyin = '';
//单个汉字拼音的字典
private static $dic = array(
'腌' => 'yan',
'嗄' => 'a',
'迫' => 'po',
'捱' => 'ai',
'艾' => 'ai',
'瑷' => 'ai',
'嗌' => 'ai',
'犴' => 'an',
'鳌' => 'ao',
'廒' => 'ao',
'拗' => 'niu',
'岙' => 'ao',
'鏊' => 'ao',
'扒' => 'ba',
'岜' => 'ba',
'耙' => 'pa',
'鲅' => 'ba',
'癍' => 'ban',
'膀' => 'pang',
'磅' => 'bang',
'炮' => 'pao',
'曝' => 'pu',
'刨' => 'pao',
'瀑' => 'pu',
'陂' => 'bei',
'埤' => 'pi',
'鹎' => 'bei',
'邶' => 'bei',
'孛' => 'bei',
'鐾' => 'bei',
'鞴' => 'bei',
'畚' => 'ben',
'甏' => 'beng',
'舭' => 'bi',
'秘' => 'mi',
'辟' => 'pi',
'泌' => 'mi',
'襞' => 'bi',
'跸' => 'bi',
'笾' => 'bian',
'扁' => 'bian',
'碥' => 'bian',
'窆' => 'bian',
'便' => 'bian',
'弁' => 'bian',
'缏' => 'bian',
'骠' => 'biao',
'杓' => 'shao',
'飚' => 'biao',
'飑' => 'biao',
'瘭' => 'biao',
'髟' => 'biao',
'玢' => 'bin',
'豳' => 'bin',
'镔' => 'bin',
'膑' => 'bin',
'屏' => 'ping',
'泊' => 'bo',
'逋' => 'bu',
'晡' => 'bu',
'钸' => 'bu',
'醭' => 'bu',
'埔' => 'pu',
'瓿' => 'bu',
'礤' => 'ca',
'骖' => 'can',
'藏' => 'cang',
'艚' => 'cao',
'侧' => 'ce',
'喳' => 'zha',
'刹' => 'sha',
'鲳' => 'chang',
'长' => 'chang',
'苌' => 'chang',
'氅' => 'chang',
'鬯' => 'chang',
'焯' => 'chao',
'朝' => 'chao',
'车' => 'che',
'琛' => 'chen',
'谶' => 'chen',
'榇' => 'chen',
'蛏' => 'cheng',
'埕' => 'cheng',
'枨' => 'cheng',
'塍' => 'cheng',
'裎' => 'cheng',
'螭' => 'chi',
'眵' => 'chi',
'墀' => 'chi',
'篪' => 'chi',
'坻' => 'di',
'瘛' => 'chi',
'种' => 'zhong',
'重' => 'zhong',
'仇' => 'chou',
'帱' => 'chou',
'俦' => 'chou',
'雠' => 'chou',
'臭' => 'chou',
'楮' => 'chu',
'畜' => 'chu',
'嘬' => 'zuo',
'膪' => 'chuai',
'巛' => 'chuan',
'椎' => 'zhui',
'呲' => 'ci',
'兹' => 'zi',
'伺' => 'si',
'璁' => 'cong',
'楱' => 'cou',
'攒' => 'zan',
'爨' => 'cuan',
'隹' => 'zhui',
'榱' => 'cui',
'撮' => 'cuo',
'鹾' => 'cuo',
'嗒' => 'da',
'哒' => 'da',
'沓' => 'ta',
'骀' => 'tai',
'绐' => 'dai',
'埭' => 'dai',
'甙' => 'dai',
'弹' => 'dan',
'澹' => 'dan',
'叨' => 'dao',
'纛' => 'dao',
'簦' => 'deng',
'提' => 'ti',
'翟' => 'zhai',
'绨' => 'ti',
'丶' => 'dian',
'佃' => 'dian',
'簟' => 'dian',
'癜' => 'dian',
'调' => 'tiao',
'铞' => 'diao',
'佚' => 'yi',
'堞' => 'die',
'瓞' => 'die',
'揲' => 'die',
'垤' => 'die',
'疔' => 'ding',
'岽' => 'dong',
'硐' => 'dong',
'缍' => 'duo',
'锷' => 'e',
'鹗' => 'e',
'阏' => 'e',
'诶' => 'ea',
'鲕' => 'er',
'珥' => 'er',
'佴' => 'er',
'番' => 'fan',
'彷' => 'pang',
'霏' => 'fei',
'蜚' => 'fei',
'鲱' => 'fei',
'芾' => 'fei',
'瀵' => 'fen',
'鲼' => 'fen',
'否' => 'fou',
'趺' => 'fu',
'桴' => 'fu',
'莩' => 'fu',
'菔' => 'fu',
'幞' => 'fu',
'郛' => 'fu',
'绂' => 'fu',
'绋' => 'fu',
'祓' => 'fu',
'砩' => 'fu',
'黻' => 'fu',
private static function mbStringToArray($string)
{
$stop = mb_strlen($string, 'utf-8');
$result = array();
for ($idx = 0; $idx < $stop; $idx++) {
$result[] = mb_substr($string, $idx, 1, 'utf-8');
}
return $result;
}
/
汉字转拼音
@param $string
@param $encoding
/
private static function chineseToPinyin($string, $encoding)
{
$words = self::mbStringToArray(mb_convert_encoding($string, 'utf-8', $encoding));
self::$string = $string;
self::$encoding = $encoding;
self::$pinyin = '';
self::$short_pinyin = '';
foreach ($words as $v) {
if (isset(self::$dic[$v])) {
$tmp = self::$dic[$v];
} else {
$tmp = $v;
}
self::$pinyin .= $tmp;
self::$short_pinyin .= mb_substr($tmp, 0, 1, $encoding);
}
}
/
获取拼音
@param $string
@param string $encoding
@return string
/
public static function getPinyin($string, $encoding = 'utf-8')
{
if ($string != self::$string || $encoding != self::$encoding) {
self::chineseToPinyin($string, $encoding);
}
return self::$pinyin;
}
/
获取拼音缩写
@param $string
@param string $encoding
@return string
/
public static function getShortPinyin($string, $encoding = 'utf-8')
{
if ($string != self::$string || $encoding != self::$encoding) {
self::chineseToPinyin($string, $encoding);
}
return self::$short_pinyin;
}
}