php
function pinyin($s, $isfirst = false) { static $pinyins; $s = trim($s); $len = strlen($s); if($len < 3) return $s; if(!isset($pinyins)) { $data = '一:yi|丁:ding|丂:kao|七:qi|丄:shang|丅:xia|丆:mu|萬:wan|丈:zhang|三:san|上:shang|下:xia|丌:ji|不:bu|與:yu|丏:mian|丐:gai|醜:chou|丒:chou|專:zhuan|且:qie|丕:pi|世:shi|丗:shi|丘:qiu|丙:bing|業:ye|叢:cong|東:dong|絲:si|丞:cheng|丟:diu|丠:qiu|両:liang|丟:diu|丣:you|兩:liang|嚴:yan|並:bing|喪:sang|丨:gun|丩:jiu|個:ge|丫:ya|爿:zhuang|中:zhong|丮:ji|丯:jie|豐:feng|丱:guan|串:chuan|丳:chan|臨:lin|丵:zhuo|丶:zhu|丷:ha|丸:wan|丹:dan|為:wei|主:zhu|丼:jing|麗:li|舉:ju|丿:pie|乀:fu|乁:yi|乂:yi|乃:nai|乄:wu|久:jiu|乆:jiu|乇:tuo|麼:me|義:yi|乊:ho|之:zhi|烏:wu|乍:zha|乎:hu|乏:fa|樂:le|乑:yin|乒:ping|乓:pang|喬:qiao|乕:hu|乖:guai|乗:cheng'; $a1 = explode('|', $data); $pinyins = array(); foreach($a1 as $v) { $a2 = explode(':', $v); $pinyins[$a2[0]] = $a2[1]; } } $rs = ''; for($i = 0; $i < $len; $i++) { $o = ord($s[$i]); if($o < 0x80) { if(($o >= 48 && $o <= 57) || ($o >= 97 && $o <= 122)) { $rs .= $s[$i]; // 0-9 a-z }elseif($o >= 65 && $o <= 90) { $rs .= strtolower($s[$i]); // A-Z }else{ $rs .= '_'; } }else{ $z = $s[$i].$s[++$i].$s[++$i]; if(isset($pinyins[$z])) { $rs .= $isfirst ? $pinyins[$z][0] : $pinyins[$z]; }else{ $rs .= '_'; } } } return $rs;}