php的UTF-8中文拼音排序

    因为我的记忆力严重不足,所以这几天写了一个UTF-8的备忘录,结果发现在UTF-8的编码下,mysql不能正确地排序,于是换到php里面用sort,继续失败……汗死……后来一想,可能是因为UTF-8的编码不是按照拼音来编码的,所以才会导致这个原因,另外貌似GBK的字库也不是完全按拼音排序的,"窦"字在GBK里面存在,但是却排在最后面,为了避免错乱,故在排序时把"窦"替换成同音字"豆",即可,于是写了这个排序函数……哎,弄了我一个晚上,结果就8行代码……无语了……

PHP代码
  1. function name_cmp($a$b) {   
  2.  $a = str_replace(‘窦’‘豆’$a);   
  3.  $b = str_replace(‘窦’‘豆’$b);   
  4.  $a = iconv(‘UTF-8′‘GBK’$a);   
  5.  $b = iconv(‘UTF-8′‘GBK’$b);   
  6.  $a = ereg_replace(‘^(a|an|the) ’strtolower($a));   
  7.  $b = ereg_replace(‘^(a|an|the) ’strtolower($b));   
  8.  return strcasecmp($a$b);   
  9. }   
  10.   
  11. usort($array‘name_cmp’);   

关于网上的mysql按拼音查询的两种说法:
1. 用ORDER BY BINARY(字段) ASC的方式查询数据库
    实际上在纯UTF-8的环境下是实现不了了,只有用UTF-8的编码去保存GBK的字符之后才能这样使用,而且不能忘记替换像"窦"这种特殊的字哦……

2. 在数据库中新建一个字段,保存汉字拼音,再利用该字段查询
    这个方法在数据库里面看起来比较直观,不过同样需要把汉字转换成拼音,直接获得拼音的首字母还是不够的…….
    在网上找了份代码,运行的还不错,不过需要下载一个字库文件:
   gbk拼音字库.rar (92)
    预览地址:http://www.bigasp.com.cn/extra/gbk2py
    代码如下: 

PHP代码
  1. class my_Getpy {   
  2.  var $_dat = ‘py.dat’;   
  3.  var $_fd  = false;   
  4.   
  5.  function my_Getpy($pdat = ) {   
  6.   if ( != $pdat)   
  7.   $this->_dat = $pdat;   
  8.  }   
  9.   
  10.  function load($pdat = ) {   
  11.   if ( == $pdat)   
  12.   $pdat = $this->_dat;   
  13.   $this->unload();   
  14.   $this->_fd = @fopen($pdat‘rb’);   
  15.   if (!$this->_fd) {   
  16.    trigger_error("unable to load PinYin data file `$pdat`", E_USER_WARNING);   
  17.    return false;   
  18.   }   
  19.   return true;   
  20.  }   
  21.   
  22.  function unload() {   
  23.   if ($this->_fd) {   
  24.    @fclose($this->_fd);   
  25.    $this->_fd = false;   
  26.   }   
  27.  }   
  28.   
  29.  function get($zh) {   
  30.   if (strlen($zh) != 2) {   
  31.    trigger_error("`$zh` is not a valid GBK hanzi", E_USER_WARNING);   
  32.    return false;   
  33.   }   
  34.   if (!$this->_fd && !$this->load()) return false;   
  35.   $high = ord($zh[0]) - 0×81;   
  36.   $low  = ord($zh[1]) - 0×40;   
  37.   // 计算偏移位置   
  38.   $nz = ($ord0 - 0×81);   
  39.   $off = ($high<<8) + $low - ($high * 0×40);   
  40.   // 判断 off 值   
  41.   if ($off < 0) {   
  42.    trigger_error("`$zh` is not a valid GBK hanzi-2", E_USER_WARNING);   
  43.    return false;   
  44.   }   
  45.   fseek($this->_fd, $off * 8, SEEK_SET);   
  46.   $ret = fread($this->_fd, 8);   
  47.   $ret = unpack(‘a8py’$ret);   
  48.   return $ret['py'];   
  49.  }   
  50.   
  51.  function _my_Getpy() {   
  52.   $this->_unload();   
  53.  }   
  54. }
  55. // demo 测试例子  
  56. ?>   
  57.   
  58. <title>GBK码汉字转拼音</title>   
  59. <h1>GBK码汉字转拼音</h1>   
  60.   
  61. <form method=get>   
  62. 输入汉字试试:<input type="text" size="16" name="zh">   
  63. <input type="submit">   
  64. </form>   
  65.   
  66. <?php   
  67. if ($str = $_GET['zh']) {   
  68.  $py = new my_Getpy;       
  69.  $len = strlen($str);   
  70.  $ret = ;   
  71.  for ($i = 0; $i < $len$i++) {   
  72.   if (ord($str[$i]) > 0×80) {   
  73.    $xx = $py->get(substr($str$i, 2));   
  74.    $ret .= ($xx ?  $xx . ‘ ’ : substr($str$i, 2));       
  75.    $i++;   
  76.   } else {   
  77.    $ret .= $str[$i];   
  78.   }   
  79.  }   
  80.  $py->unload();   
  81.  echo "字串 `<font color=red>{$str}</font>` 的拼音是: <font color=red>{$ret}</font>\n";   
  82. }   
  83. ?>    
Posted in 03 Binary Life. Tags: , , . 10 条评论 »