php漢字轉拼音的樣本_php技巧

來源:互聯網
上載者:User

複製代碼 代碼如下:

<?php
class Helper_Spell{
    public $spellArray = array();

    static public function getArray() {
        return unserialize(file_get_contents('pytable_without_tune.txt'));
    }
    /**
     * @desc 擷取字串的首字母
     * @param $string 要轉換的字串
     * @param $isOne 是否取首字母
     * @param $upper 是否轉換為大寫
     * @return string
     *
     * 例如:getChineseFirstChar('我是作者') 首字元全部字母+小寫
     * return "wo"
     *
     * 例如:getChineseFirstChar('我是作者',true) 首字元首字母+小寫
     * return "w"
     *
     * 例如:getChineseFirstChar('我是作者',true,true) 首字元首字母+大寫
     * return "W"
     *
     * 例如:getChineseFirstChar('我是作者',false,true) 首字元全部字母+大寫
     * return "WO"
     */
    static public function getChineseFirstChar($string,$isOne=false,$upper=false) {
        $spellArray = self::getArray();
        $str_arr = self::utf8_str_split($string,1); //將字串拆分成數組

        if(preg_match('/^[\x{4e00}-\x{9fa5}]+$/u',$str_arr[0])) { //判斷是否是漢字
            $chinese = $spellArray[$str_arr[0]];
            $result = $chinese[0];
        }else {
            $result = $str_arr[0];
        }

        $result = $isOne ? substr($result,0,1) : $result;

        return $upper?strtoupper($result):$result;
    }

    /**
     * @desc 將字串轉換成拼音字串
     * @param $string 漢字字串
     * @param $upper 是否大寫
     * @return string
     *
     * 例如:getChineseChar('我是作者'); 全部字串+小寫
     * return "wo shi zuo zhe"
     *
     * 例如:getChineseChar('我是作者',true); 首字母+小寫
     * return "w s z z"
     *
     * 例如:getChineseChar('我是作者',true,true); 首字母+大寫
     * return "W S Z Z"
     *
     * 例如:getChineseChar('我是作者',false,true); 首字母+大寫
     * return "WO SHI ZUO ZHE"
     */
    static public function getChineseChar($string,$isOne=false,$upper=false) {
        global $spellArray;
        $str_arr = self::utf8_str_split($string,1); //將字串拆分成數組
        $result = array();
        foreach($str_arr as $char)
        {
            if(preg_match('/^[\x{4e00}-\x{9fa5}]+$/u',$char))
            {
                $chinese = $spellArray[$char];
                $chinese  = $chinese[0];
            }else{
                $chinese=$char;
            }
            $chinese = $isOne ? substr($chinese,0,1) : $chinese;
            $result[] = $upper ? strtoupper($chinese) : $chinese;
        }
        return implode(' ',$result);
    }
    /**
     * @desc 將字串轉換成數組
     * @param $str 要轉換的數組
     * @param $split_len
     * @return array
     */
    private function utf8_str_split($str,$split_len=1) {

        if(!preg_match('/^[0-9]+$/', $split_len) || $split_len < 1) {
            return FALSE;
        }

        $len = mb_strlen($str, 'UTF-8');

        if ($len <= $split_len) {
            return array($str);
        }
        preg_match_all('/.{'.$split_len.'}|[^\x00]{1,'.$split_len.'}$/us', $str, $ar);

        return $ar[0];
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.