PHP 实现数字转字母,生成Excel列标

来源:互联网 发布:论文抄袭软件 编辑:程序博客网 时间:2024/09/21 08:19
/**
 * 数字转字母 (类似于Excel列标)
 * @param Int $index 索引值
 * @param Int $start 字母起始值
 * @return String 返回字母
 */
function IntToChr($index, $start = 65) {
    $str = '';
    if (floor($index / 26) > 0) {
        $str .= IntToChr(floor($index / 26)-1);
    }
    return $str . chr($index % 26 + $start);
}
 
/**
 * 测试
 */
function test() {
    echo IntToChr(0); //# A
    echo IntToChr(1); //# B
    // ...
    echo IntToChr(27); //# AB
}