Char.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. class Char{
  3. protected static $letters = array(
  4. "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
  5. "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"
  6. );
  7. protected static $zhNums = array(
  8. "零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十",
  9. );
  10. protected static $legal = array("UPPER", "LOWER");
  11. public static function numToLetter($num, $begain = 0, $case = "UPPER"){
  12. if(!in_array($case, self::$legal))
  13. trigger_error('param 3 must be one of "UPPER", "LOWER"');
  14. $num = (int)$num;
  15. $begain = (int)$begain;
  16. if(is_int($num) && is_int($begain) && $case){
  17. if($begain < 0) $begain = 0;
  18. $num = $num - $begain;
  19. if($num < 0) $num = 0;
  20. else if($num > 25) $num = 25;
  21. $letter = self::$letters[$num];
  22. if($case === "UPPER")
  23. $letter = strtoupper($letter);
  24. return $letter;
  25. }
  26. return $num;
  27. }
  28. public static function letterToNum($letter, $begain = 0){
  29. $letter = strtolower($letter);
  30. $rs = array_keys(self::$letters, $letter, true);
  31. if($rs)
  32. $position = current($rs) + $begain;
  33. else
  34. $position = null;
  35. return $position;
  36. }
  37. public static function numToZh($num){
  38. $len = count(self::$zhNums) - 1;
  39. $num = (int)$num;
  40. if($num < 0) $num = 0;
  41. if($num > $len) return null;
  42. return self::$zhNums[$num];
  43. }
  44. }