php程序如何把中文字符转换为拼音(3)
"|-10764|-10587|-10544|-10533|-10519|-10331|-10329|-10328|-10322|-10315|-10309|-10307|-10296|-10281|-10274".
"|-10270|-10262|-10260|-10256|-10254";
$tDataKey = explode('|', $dataKey);
$tDataValue = explode('|', $dataValue);
$data = (PHP_VERSION>='5.0') ? array_combine($tDataKey, $tDataValue) : $this->arrayCombine($tDataKey, $tDataValue);
arsort($data);
reset($data);
if($charset != 'gb2312') $string = $this->utf82GB2312($string);
$res = '';
for($i=0; $i<strlen($string); $i++)
{
$asc = ord(substr($string, $i, 1));
if($asc>160) { $q = ord(substr($string, ++$i, 1)); $asc = $asc*256 + $q - 65536; }
$res .= $this->asc2Pinyin($asc, $data);
}
return preg_replace("/[^a-z0-9]*/", '', $res);
}
/**
* 把ASCII码转换为对应的拼音
* @param $asc ASCII码
* @param $data ASCII码拼音字符对照表
*/
public function asc2Pinyin($asc, $data)
{
if ($asc>0 && $asc<160 ) return chr($asc);
elseif($asc<-20319 || $asc>-10247) return '';
else {
foreach($data as $k=>$v){ if($v<=$asc) break; }
return $k;
}
}
/**
* 将UFT8字符的编码转化为GB2312
* @param $char
*/
public function utf82GB2312($char)
{
$string = '';
if($char < 0x80) $string .= $char;
elseif($char < 0x800)
相关新闻>>
- 发表评论
-
- 最新评论 更多>>