歡迎來到小豬圈!

2009-04-21

查注音函式

  • 利用 xcin 的表格做查詢注音符號的依據。
    
    <?php
    function findZhuyins($word, $cinTable = 'bopomofo.cin') {
      static $data = null;
      static $keyMap = null;
      $result = array();
      if (!$data) {
        $data = file_get_contents($cinTable);
        $pattern = "/%keyname\s+begin\s+(.+?)\s+%keyname\s+end/uis"; //找出按鍵對應字根的區段
        preg_match($pattern, $data, $matches);
        $pattern = "/(.)\s+(.)\s+/ui";
        preg_match_all($pattern, $matches[1], $matches );
        $keyMap = $matches;
      }
      $pattern = "/\n(.+)\s+{$word}/ui";
      preg_match_all($pattern, $data, $matches );
      foreach ($matches[1] as $key => $value) {
        $ary = str_split($value);
        $result[$key] = '';
        foreach ($ary as $value2) {
          $idx = array_search($value2, $keyMap[1]);
          $result[$key] .= $keyMap[2][$idx];
        }
      }
      return $result;
    }
    var_dump( findZhuyins("得"));
    ?>
    
    
  • 注音符號的 cin 表格可以到 OXIM 專案的 FTP(ftp://ftp.opendesktop.org.tw/odp/others/OXIM/cin_tables/)下載,cin 表格清單。
  • 中文轉注音(PHP)

2 comments:

Comment Form Message

標籤分類

Blog Archive

Labels

Google Analytics Tracking Code

About Me

My photo
Keelung, R.O.C, Taiwan
一個不學無術、混吃等死的傢伙…