乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      PHP漢字轉(zhuǎn)拼音函數(shù)類示例

       馬超讀書的季節(jié) 2015-12-25
      在PHP中如何將中文漢字轉(zhuǎn)換成對(duì)應(yīng)的拼音或者首字母等功能呢,現(xiàn)在我們就來講解一下PHP所提供的漢字轉(zhuǎn)拼音類。


          首先要實(shí)現(xiàn)這功能需要包含一個(gè)ChinesePinyin.class.php類

      代碼:

      <?php
      /**
      *
      * 漢字轉(zhuǎn)拼音類
      * @Author : Kin
      * @Date   : 2014-03-16
      * @Email  : Mr.kin@foxmail.com
      *
      */
      namespace Org\Util;
      define('PINYIN_ROOT'dirname(__FILE__));
      class ChinesePinyin{
       
          //utf-8中國(guó)漢字集合
          private $ChineseCharacters;
          //編碼
          private $charset = 'utf-8';
       
          public function __construct(){
              if( empty($this->ChineseCharacters) ){
                $this->ChineseCharacters = file_get_contents(PINYIN_ROOT.'/Pinyin/ChineseCharacters.dat');
              }
          }
       
          /*
          * 轉(zhuǎn)成帶有聲調(diào)的漢語拼音
          * param $input_char String  需要轉(zhuǎn)換的漢字
          * param $delimiter  String   轉(zhuǎn)換之后拼音之間分隔符
          * param $outside_ignore  Boolean     是否忽略非漢字內(nèi)容
          */
          public function TransformWithTone($input_char,$delimiter=' ',$outside_ignore=false){
       
              $input_len = mb_strlen($input_char,$this->charset);
              $output_char = '';
              for($i=0;$i<$input_len;$i++){
                  $word = mb_substr($input_char,$i,1,$this->charset);
                  if(preg_match('/^[\x{4e00}-\x{9fa5}]$/u',$word) && preg_match('/\,'.preg_quote($word).'(.*?)\,/',$this->ChineseCharacters,$matches) ){
                      $output_char.=$matches[1].$delimiter;
                  }else if(!$outside_ignore){
                      $output_char.=$word;
                  }
              }
       
              return $output_char;
          }
       
          /*
          * 轉(zhuǎn)成帶無聲調(diào)的漢語拼音
          * param $input_char String  需要轉(zhuǎn)換的漢字
          * param $delimiter  String   轉(zhuǎn)換之后拼音之間分隔符
          * param $outside_ignore  Boolean     是否忽略非漢字內(nèi)容
          */
          public function TransformWithoutTone($input_char,$delimiter='',$outside_ignore=true){
       
              $char_with_tone = $this->TransformWithTone($input_char,$delimiter,$outside_ignore);
       
              $char_without_tone  =  str_replace(array('ā','á','ǎ','à','ō','ó','ǒ','ò','ē','é','ě','è','ī','í','ǐ','ì','ū','ú','ǔ','ù','ǖ','ǘ','ǚ','ǜ','ü'),
                                                 array('a','a','a','a','o','o','o','o','e','e','e','e','i','i','i','i','u','u','u','u','v','v','v','v','v')
                                                 ,$char_with_tone );
              return $char_without_tone;
       
          }
       
          /*
          * 轉(zhuǎn)成漢語拼音首字母,只包括漢字
          * param $input_char String  需要轉(zhuǎn)換的漢字
          * param $delimiter  String   轉(zhuǎn)換之后拼音之間分隔符
          */
          public function TransformUcwordsOnlyChar($input_char,$delimiter=''){
       
              $char_without_tone = ucwords($this->TransformWithoutTone($input_char,' ',true));
              $ucwords = preg_replace('/[^A-Z]/','',$char_without_tone);
              if(!empty($delimiter)){
                  $ucwords = implode($delimiter,str_split($ucwords));
              }
              return $ucwords;
       
       
          }
       
       
          /*
          * 轉(zhuǎn)成漢語拼音首字母,包含非漢字內(nèi)容
          * param $input_char String  需要轉(zhuǎn)換的漢字
          * param $delimiter  String   轉(zhuǎn)換之后拼音之間分隔符
          */
          public function TransformUcwords($input_char,$delimiter=' ',$outside_ignore=false){
       
              $input_len = mb_strlen($input_char,$this->charset);
              $output_char = '';
              for($i=0;$i<$input_len;$i++){
                  $word = mb_substr($input_char,$i,1,$this->charset);
                  if(preg_match('/^[\x{4e00}-\x{9fa5}]$/u',$word) && preg_match('/\,'.preg_quote($word).'(.*?)\,/',$this->ChineseCharacters,$matches) ){
                      $output_char.=$matches[1].$delimiter;
                  }else if(!$outside_ignore){
                      $output_char.= $delimiter.$word.$delimiter;
                  }
              }
              $output_char  =  str_replace(array('ā','á','ǎ','à','ō','ó','ǒ','ò','ē','é','ě','è','ī','í','ǐ','ì','ū','ú','ǔ','ù','ǖ','ǘ','ǚ','ǜ','ü'),
                                                 array('a','a','a','a','o','o','o','o','e','e','e','e','i','i','i','i','u','u','u','u','v','v','v','v','v')
                                                 ,$output_char );
       
              $array = explode($delimiter,$output_char);
              $array = array_filter($array);
              $res = '';
              foreach($array as $list){
                  $res .= substr($list,0,1);
              }
              return $res;
          }
       
       
       
       
      }



      我們來看一下如何調(diào)用這個(gè)類,并且操作這個(gè)類


      以下是index.php文件

      代碼:

      <?php
      include 'ChinesePinyin.class.php';
      $Pinyin = new \Org\Util\ChinesePinyin();
      header("Content-Type:text/html;charset=utf-8");
      $str = $_POST['str'];
      if(strlen($str)<=0){
          echo '請(qǐng)輸入要轉(zhuǎn)換的內(nèi)容';
          exit;
      }
      $pinyin1 = $Pinyin->TransformWithTone($str);
      $pinyin2 = $Pinyin->TransformWithoutTone($str);
      $pinyin3 = $Pinyin->TransformUcwordsOnlyChar($str);
      $pinyin4 = $Pinyin->TransformUcwords($str);
      echo '帶聲調(diào)的漢語拼音: <span class="red">'.$pinyin1.'</span>';
      echo '<br>';
      echo '無聲調(diào)的漢語拼音: <span class="red">'.$pinyin2.'</span>';
      echo '<br>';
      echo '首字母只包括漢字: <span class="red">'.$pinyin3.'</span>';
      echo '<br>';
      echo '首字母和其他字符: <span class="red">'.$pinyin4.'</span>';
      echo '<br>';
       
      ?>


      把這兩個(gè)文件放同一目錄下,然后通過include函數(shù)包含,再通過實(shí)例化就能調(diào)用里面相應(yīng)的方法了。


      至于每個(gè)方法的功能ChinesePinyin.class.php類里面都有注釋,自己可以認(rèn)真看一下,我們也可以通過示例來看一下效果

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多