PHP Kanji to Pinyin
PHP Functions (modified by the Spgetpinyin function of Dedecms (dedecms/include/inc/inc_fun_funadmin.php), dedecms dictionaries are not quite complete):
PhpfunctionPinyin$str,$ishead) { Static $pinyins=Array(); $restr= ''; $str=Trim($str); $slen=strlen($str); if($slen< 2) { return $str; } if(Count($pinyins) = = 0) { $fp=fopen(' Pinyin.dic ', ' R '); while(!feof($fp)) { $line=Trim(fgets($fp)); $a 2=Explode('`',$line); isset($a 2[1]) &&$pinyins[$a 2[0]] =$a 2[1]; } fclose($fp); } for($i= 0;$i<$slen;$i++) { if(Ord($str[$i]) >0x80) { $c=$str[$i].$str[$i+1]; $i++; if(isset($pinyins[$c])) { $restr.= ($ishead==0)?$pinyins[$c]:$pinyins[$c][0]; }Else { $restr.= "_"; } }Else if(Preg_match("/[a-z0-9]/i",$str[$i]) ) { $restr.=$str[$i]; } Else { $restr.= "_"; } } return $restr; } //Test EchoPinyin (' Shu Jia insulation film ', 1), '
'; EchoPinyin (' Shu Jia insulation film ', 0), '
'; EchoPinyin (' Parrot ', 1), '
'; EchoPinyin (' parrot ', 0), '
'; EchoPinyin (' Sleeping fort/Innovation/myside Mattress ', 1), '
'; EchoPinyin (' Sleeping fort/Innovation/myside Mattress ', 0), '
'; /*results: srjgrm shurejiageremo yw yingwu mzb_ynws_mysidecd mianzhibao_yinuoweishen_mysidechuangdian*/?>
The included dictionaries are attached: pinyin.rar/pinyin.dic (GBK)
Download Address: Http://files.cnblogs.com/luoyunshu/pinyin.rar
Reference:
http://www.oschina.net/code/snippet_862384_25415
http://www.bkjia.com/PHPjc/822275.html www.bkjia.com true http://www.bkjia.com/PHPjc/822275.html techarticle PHP kanji to pinyin php function (modified by DEDECMS (dedecms/include/inc/inc_fun_funadmin.php) spgetpinyin function, Dedecms dictionary is not quite complete): PHP function Pinyin ($str, $is ...