php漢字轉拼音

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   cti   

php漢字轉拼音

php函數(由dedecms(dedecms/include/inc/inc_fun_funAdmin.php)的SpGetPinyin函數修改,dedecms的字典不太完全):

<?php    function pinyin($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));                $a2 = explode(‘`‘, $line);                isset($a2[1]) && $pinyins[$a2[0]] = $a2[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;    }        //測試    echo pinyin(‘舒熱佳隔熱膜‘,1),‘<br>‘;     echo pinyin(‘舒熱佳隔熱膜‘,0),‘<br>‘;     echo pinyin(‘鸚鵡‘,1),‘<br>‘;      echo pinyin(‘鸚鵡‘,0),‘<br>‘;      echo pinyin(‘眠之堡/依諾維紳/myside床墊‘,1),‘<br>‘;     echo pinyin(‘眠之堡/依諾維紳/myside床墊‘,0),‘<br>‘;          /*結果:    srjgrm    shurejiageremo    yw    yingwu    mzb_ynws_mysidecd    mianzhibao_yinuoweishen_mysidechuangdian*/?>

包含的字典見附件:pinyin.rar/pinyin.dic (gbk)

:http://files.cnblogs.com/luoyunshu/pinyin.rar

參考:

http://www.oschina.net/code/snippet_862384_25415

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.