PHP基于自定义函数实现的汉字转拼音功能实例
发布时间:2021-03-05 12:46:28 所属栏目:PHP教程 来源:网络整理
导读:本篇章节讲解PHP基于自定义函数实现的汉字转拼音功能。分享给大家供大家参考,具体如下: 整个过程用到了pinyin.table文件。 pinyin.php 160) { $q = ord(substr($text,++$i,1)); $p = $p*256+$q-65536; } if($p > 0 $p -10247) { $r[$k] = ''; } else { f
本篇章节讲解PHP基于自定义函数实现的汉字转拼音功能。分享给大家供大家参考,具体如下: 整个过程用到了pinyin.table文件。 pinyin.php 160) {
$q = ord(substr($text,++$i,1));
$p = $p*256+$q-65536;
}
if($p > 0 && $p < 160) {
$r[$k] = chr($p);
} elseif($p< -20319 || $p > -10247) {
$r[$k] = '';
} else {
for($j = $tmps-1; $j >= 0; $j--) {
if($data[$j][1]<=$p) break;
}
$r[$k] = $data[$j][0];
}
$k++;
}
return implode($exp,$r);
}
function convert($str,$from = 'utf-8',$to = 'gb2312') {
if(!$str) return '';
$from = strtolower($from);
$to = strtolower($to);
if($from == $to) return $str;
$from = str_replace('gbk','gb2312',$from);
$to = str_replace('gbk',$to);
$from = str_replace('utf8','utf-8',$from);
$to = str_replace('utf8',$to);
if($from == $to) return $str;
$tmp = array();
if(function_exists('iconv')) {
if(is_array($str)) {
foreach($str as $key => $val) {
$tmp[$key] = iconv($from,$to."//IGNORE",$val);
}
return $tmp;
} else {
return iconv($from,$str);
}
} else if(function_exists('mb_convert_encoding')) {
if(is_array($str)) {
foreach($str as $key => $val) {
$tmp[$key] = mb_convert_encoding($val,$to,$from);
}
return $tmp;
} else {
return mb_convert_encoding($str,$from);
}
} else {
require_once 'convert.func.php';
return dconvert($str,$from);
}
}
?>
pinyin.table 备注:新建text文件复制下面代码到文件,重命名文件名pinyin.tablePS:这里再为大家提供几款本站拼音与字母相关工具供大家参考: 在线中英文根据首字母排序工具: 在线汉字转换成拼音工具: 在线字母大小写转换工具: 更多关于PHP相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》及《》 希望本文所述对大家PHP程序设计有所帮助。 (编辑:滁州站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |