Using PHP, how can I convert an Excel style column name, like "CF", into a number (CF=84)? This question specifically pertains to PHP. Please don't "dupe" this with an answer for a different language.
            Asked
            
        
        
            Active
            
        
            Viewed 886 times
        
    0
            
            
        - 
                    2dupe: http://stackoverflow.com/questions/5554369/php-how-to-output-list-like-this-aa-ab-ac-all-the-way-to-zzzy-zzzz-zzzza – Mar 16 '17 at 20:33
- 
                    Possible duplicate of [Excel column number from column name](http://stackoverflow.com/questions/10106465/excel-column-number-from-column-name) – MarianD Mar 16 '17 at 22:16
- 
                    @nogad, possibly but when I was searching for a solution I did not find that question because it is for a different utility. – azBrian Mar 16 '17 at 23:38
- 
                    @MarianD, It is not a duplicate of that question. This Q&A specifically is related to PHP use. – azBrian Mar 16 '17 at 23:39
- 
                    1Oh, you are right, please accept my apology. – MarianD Mar 17 '17 at 16:19
1 Answers
1
            
            
        Try these functions.
/**
 * Convert Excel style column names into numbers.
 * @param string $column Excel style column name like AA or CF
 * @return integer Number with A being 1 and AA being 27
 */
function alpha2num($column) {
    $number = 0;
    foreach(str_split($column) as $letter){
        $number = ($number * 26) + (ord(strtolower($letter)) - 96);
    }
    return $number;
}
/**
 * Access array value with Excel style column name.
 * @param array $array The array to access
 * @param string $column Excel style column name like AA or CF
 * @return mixed Value found at the column
 */
function alpha_col($array, $column){
    $i = alpha2num($column) - 1;
    return isset($array[$i]) ? $array[$i] : false;
}
 
    
    
        azBrian
        
- 661
- 1
- 5
- 19
- 
                    2[PHPExcel does it a bit differently, if you're interested.](https://github.com/PHPOffice/PHPExcel/blob/1c8c2379ccf5ab9dd7cb46be965821d22173bcf4/Classes/PHPExcel/Cell.php#L790) – Don't Panic Mar 16 '17 at 20:54
- 
                    
- 
                    1I just thought you might be interested in some of their observations on `ord` and `strtolower` performance impacts. I agree your way looks neater. – Don't Panic Mar 16 '17 at 23:46
- 
                    If you were building a library, like PHPExcel did, I would be more concerned with performance. When I wrote the functions I was working on a simple CSV import system. – azBrian Mar 16 '17 at 23:49
- 
                    1Makes sense. I didn't intend to be critical. I just thought you'd find it interesting if you hadn't seen it already. – Don't Panic Mar 16 '17 at 23:52
- 
                    
- 
                    Note: the code PHPExcel uses in the link above is no longer valid in newer PHP versions - 'Deprecated: Array and string offset access syntax with curly braces is deprecated' – kasimir Jul 16 '21 at 13:26