把汉字转化为拼音,(最笨也是最准确的办法)
程序员文章站
2023-12-30 23:43:22
...
2. [代码][PHP]代码 跳至
<?php /** * 本文作者:chinesehero@163.com * 获取一个汉字的拼音 * 用法如下: * include('Pinyin.php'); * echo getPinyin("你好,吃了吗?"); */ function getPinyin($keyWord) { $hz['腌']="yan"; $hz['嗄']="a"; $hz['迫']="po"; $hz['捱']="ai"; $hz['艾']="ai"; //此处省略6900余字 $hz['蜱']="pi"; $hz['螋']="sou"; $hz['螗']="tang"; $hz['螵']="piao"; $hz['蟛']="peng"; $result=""; $charArray=str_split_php5_utf8($keyWord); foreach($charArray as $char){ if(!empty($hz[$char])){ $result= $result.$hz[$char]; } else { $result= $result.$char; } } return $result; } function str_split_php5_utf8($str) { // place each character of the string into and array $split=1; $array = array(); for ( $i=0; $i < strlen( $str ); ){ $value = ord($str[$i]); if($value > 127){ if($value >= 192 && $value = 224 && $value = 240 && $value <= 247) $split=4; }else{ $split=1; } $key = NULL; for ( $j = 0; $j < $split; $j++, $i++ ) { $key .= $str[$i]; } array_push( $array, $key ); } return $array; } ?>