21.46. 中文排序?

Last Modify: 2004年12月12日 周日 09時47分11秒 CST

以下的筆劃排序只是概略,可依照自己的要求修改。

<?php
/*
 * Usage: bool usort(array array, "big5_cmp");
 */

function big5_storke($a)
{
  $b2h = bin2hex($a);
  $storke = array("", "a440", "a442", "a454", "a4a1", "a4fe", "a5e0", "a6ea",
    "a8c3", "ab45", "adbc", "b0ae", "b3c3", "b6c4", "b9ac", "bbf5", "bea7",
    "c075", "c24f", "c35f", "c455", "c3d7", "c56b", "c5c8", "c5f1", "c655",
    "c665", "c66c", "c676", "c67b", "c67e");
  for($i = 0; $i < sizeof($storke); $i++) {
    if($storke[$i] && $storke[$i+1]) {
      if($b2h >= $storke[$i] && $b2h < $storke[$i+1]) return $i;
    }
  }
  $storke = array("", "", "c940", "c945", "c94d", "c95d", "c9ab", "ca5a",
    "cbb1", "cddd", "d0c8", "d44b", "d851", "dcb1", "e0f0", "e4e6", "e8f4",
    "ecb9", "efb7", "f1eb", "f3fd", "f5c0", "f6d6", "f7d0", "f8a5", "f8e9",
    "f96b", "f9a2", "f9ba", "f9c6", "f9dc");
  for($i = 0; $i < sizeof($storke); $i++) {
    if($storke[$i] && $storke[$i+1]) {
      if($b2h >= $storke[$i] && $b2h < $storke[$i+1]) return $i;
    }
  }
  return 0;
}

function big5_cmp($a, $b)
{
  $a_stk = big5_storke($a);
  $b_stk = big5_storke($b);
  if($a_stk == $b_stk) {
    return 0;
  }
  return ($a_stk < $b_stk) ? -1 : 1;
}
?>

WWW: 由 堃(方方土) 探討 Big5e 編碼