php

位置:IT落伍者 >> php >> 浏览文章

PHP将汉字转换拼音


发布日期:2018年01月04日
 
PHP将汉字转换拼音

PHP将汉字转换拼音是程序员们经常需要操作的任务之一别看这一小功能却能发挥很大的作用以下是正确的汉字转拼音方法大家可以借鑒一下

网络上类似的代码大多只能在gb编码下使用下面这个类同时能在utf编码下将汉字转换为拼音具体的代码和用法如下亲测可用
<?php
function Pinyin($_String $_Code=gb)
{
$_DataKey =
"a|ai|an|ang|ao|ba|bai|ban|bang|bao|bei|ben|beng|bi|bian|biao|bie|bin|bing|bo|bu|ca|cai|can|cang|cao|ce|ceng|cha"
"|chai|chan|chang|chao|che|chen|cheng|chi|chong|chou|chu|chuai|chuan|chuang|chui|chun|chuo|ci|cong|cou|cu|"
"cuan|cui|cun|cuo|da|dai|dan|dang|dao|de|deng|di|dian|diao|die|ding|diu|dong|dou|du|duan|dui|dun|duo|e|en|er"
"|fa|fan|fang|fei|fen|feng|fo|fou|fu|ga|gai|gan|gang|gao|ge|gei|gen|geng|gong|gou|gu|gua|guai|guan|guang|gui"
"|gun|guo|ha|hai|han|hang|hao|he|hei|hen|heng|hong|hou|hu|hua|huai|huan|huang|hui|hun|huo|ji|jia|jian|jiang"
"|jiao|jie|jin|jing|jiong|jiu|ju|juan|jue|jun|ka|kai|kan|kang|kao|ke|ken|keng|kong|kou|ku|kua|kuai|kuan|kuang"
"|kui|kun|kuo|la|lai|lan|lang|lao|le|lei|leng|li|lia|lian|liang|liao|lie|lin|ling|liu|long|lou|lu|lv|luan|lue"
"|lun|luo|ma|mai|man|mang|mao|me|mei|men|meng|mi|mian|miao|mie|min|ming|miu|mo|mou|mu|na|nai|nan|nang|nao|ne"
"|nei|nen|neng|ni|nian|niang|niao|nie|nin|ning|niu|nong|nu|nv|nuan|nue|nuo|o|ou|pa|pai|pan|pang|pao|pei|pen"
"|peng|pi|pian|piao|pie|pin|ping|po|pu|qi|qia|qian|qiang|qiao|qie|qin|qing|qiong|qiu|qu|quan|que|qun|ran|rang"
"|rao|re|ren|reng|ri|rong|rou|ru|ruan|rui|run|ruo|sa|sai|san|sang|sao|se|sen|seng|sha|shai|shan|shang|shao|"
"she|shen|sheng|shi|shou|shu|shua|shuai|shuan|shuang|shui|shun|shuo|si|song|sou|su|suan|sui|sun|suo|ta|tai|"
"tan|tang|tao|te|teng|ti|tian|tiao|tie|ting|tong|tou|tu|tuan|tui|tun|tuo|wa|wai|wan|wang|wei|wen|weng|wo|wu"
"|xi|xia|xian|xiang|xiao|xie|xin|xing|xiong|xiu|xu|xuan|xue|xun|ya|yan|yang|yao|ye|yi|yin|ying|yo|yong|you"
"|yu|yuan|yue|yun|za|zai|zan|zang|zao|ze|zei|zen|zeng|zha|zhai|zhan|zhang|zhao|zhe|zhen|zheng|zhi|zhong|"
"zhou|zhu|zhua|zhuai|zhuan|zhuang|zhui|zhun|zhuo|zi|zong|zou|zu|zuan|zui|zun|zuo";
$_DataValue =
"|||||||||||||||"
"|||||||||||||||"
"|||||||||||||||"
"|||||||||||||||"
"|||||||||||||||"
"|||||||||||||||"
"|||||||||||||||"
"|||||||||||||||"
"|||||||||||||||"
"|||||||||||||||"
"|||||||||||||||"
"|||||||||||||||"
"|||||||||||||||"
"|||||||||||||||"
"|||||||||||||||"
"|||||||||||||||"
"|||||||||||||||"
"|||||||||||||||"
"|||||||||||||||"
"|||||||||||||||"
"|||||||||||||||"
"|||||||||||||||"
"|||||||||||||||"
"|||||||||||||||"
"|||||||||||||||"
"|||||||||||||||"
"|||||";
$_TDataKey = explode(| $_DataKey);
$_TDataValue = explode(| $_DataValue);
$_Data = (PHP_VERSION>=) ? array_combine($_TDataKey $_TDataValue) : _Array_Combine($_TDataKey $_TDataValue);
arsort($_Data);
reset($_Data);
if($_Code != gb) $_String = _U_Utf_Gb($_String);
$_Res = ;
for($i=; $i<strlen($_String); $i++)
{
$_P = ord(substr($_String $i ));
if($_P>) { $_Q = ord(substr($_String ++$i )); $_P = $_P* + $_Q ; }
$_Res = _Pinyin($_P $_Data);
}
return preg_replace("/[^az]*/" $_Res);
}
function _Pinyin($_Num $_Data)
{
if ($_Num> && $_Num< ) return chr($_Num);
elseif($_Num< _num="">) return ;
else {
foreach($_Data as $k=>$v){ if($v<=$_Num) break; }
return $k;
}
}
function _U_Utf_Gb($_C)
{
$_String = ;
if($_C < x) $_String = $_C;
elseif($_C < x)
{
$_String = chr(xC | $_C>>);
$_String = chr(x | $_C & xF);
}elseif($_C < x){
$_String = chr(xE | $_C>>);
$_String = chr(x | $_C>> & xF);
$_String = chr(x | $_C & xF);
} elseif($_C < x) {
$_String = chr(xF | $_C>>);
$_String = chr(x | $_C>> & xF);
$_String = chr(x | $_C>> & xF);
$_String = chr(x | $_C & xF);
}
return iconv(UTF GB $_String);
}
function _Array_Combine($_Arr $_Arr)
{
for($i=; $i<count($_Arr); $i++) $_Res[$_Arr[$i]] = $_Arr[$i];
return $_Res;
}
//用法

//第二个参数随意设置则为utf编码
echo Pinyin(聚丰);
?>

               

上一篇:解析php中curl

下一篇:为什么说PHP是个集中营