/*--------------------------
* 汉字转拼音
* $str 要转换的字符串
* $split 是否要分割显示
* $isfirst 是否返回首字母
* $zwbd 是否过滤中文标点符号
* http://files.cnblogs.com/files/helin/tools.zip
-*------------------------*/
function CHToPinyin($str,$split=true,$isfirst = false,$zwbd=true){
$py = '';
$str = trim($str);
$bd_filter = array(',','。','?','!',':','“','”',';','‘','’','《','》',
'{','}','【','】','(',')','—','〈','〉','〔','〕','﹄','﹃','﹂','﹁','~');
$str_arr = preg_split('/(?<!^)(?!$)/u', $str );
foreach($str_arr as $code){
if( in_array($code,$bd_filter) ){
if($zwbd){
$py .= $code;
}
}elseif( strlen($code) == 3 ){
//常用汉字
static $_cnfonts_min;
if(!isset($_cnfonts_min)){
include_once '_cnfonts_min.php';//$_cnfonts_min='...';
}
if(isset($_cnfonts_min[$code])){
$py .= ( $split && $py ? ' ' : '' ).($isfirst ? strtoupper($_cnfonts_min[$code][0]) : $_cnfonts_min[$code]);
}else{
//扩展汉字
static $_cnfonts_ext;
if(!isset($_cnfonts_ext)){
include_once '_cnfonts_ext.php';//$_cnfonts_ext='...';
}
if(isset($_cnfonts_ext[$code])){
$py .= ( $split && $py ? ' ' : '' ).($isfirst ? strtoupper($_cnfonts_ext[$code][0]) : $_cnfonts_ext[$code]);
}else{
$py .= $code;
}
}
}else{
$py .= $code;
}
}
return $py;
}
汉字转拼音
最新推荐文章于 2024-05-29 16:39:17 发布