序数从1开始 即 A=1 而非 A=0
1 /** 2 * 数字序列转字母序列 3 * @param $int 4 * @param int $start 5 * @return string|bool 6 */ 7 function int_to_chr_1($int, $start = 64) 8 { 9 if (!is_int($int) || $int <= 0) return false; 10 $str = ''; 11 if (floor($int / 26) > 0) { 12 $str .= int_to_chr_1((int)floor($int / 26)); 13 } 14 return $str . chr($int % 26 + $start); 15 } 16 17 /** 18 * 数字序列转字母序列 19 * @param $int 20 * @return string|bool 21 */ 22 function int_to_chr_2($int) 23 { 24 if (!is_int($int) || $int <= 0) return false; 25 26 $array = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'); 27 $str = ''; 28 if ($int > 26) { 29 $str .= int_to_chr_2((int)floor($int / 26)); 30 $str .= $array[$int % 26 - 1]; 31 return $str; 32 } else { 33 return $array[$int - 1]; 34 } 35 } 36 37 /** 38 * 字母序列转数字序列 39 * @param $char 40 * @return int|bool 41 */ 42 function chr_to_int($char) 43 { 44 //检测字符串是否全字母 45 $regex = '/^[a-zA-Z]+$/i'; 46 47 if (!preg_match($regex, $char)) return false; 48 49 $int = 0; 50 $char = strtoupper($char); 51 $array = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'); 52 $len = strlen($char); 53 for ($i = 0; $i < $len; $i++) { 54 $index = array_search($char[$i], $array); 55 $int += ($index + 1) * pow(26, $len - $i - 1); 56 } 57 return $int; 58 } 59 60 61 echo '<br>', int_to_chr_1(8848); 62 echo '<br>', int_to_chr_2(8848); 63 echo '<br>', chr_to_int('MBH');