0

I am working on script related to excel document, so I need to get column index from the column letter i.e column name. For this I don't want to use PHP Excel library, as I have working for single line of code.

for example need result like this :-

   A => 1 
   B => 2 
   AA => 27 
   AB => 28 
   EC => 132
   UMX  => 14558
ADDC
  • 103
  • 7
  • 4
    Possible duplicate of [Algorithm to get the excel-like column name of a number](https://stackoverflow.com/questions/3302857/algorithm-to-get-the-excel-like-column-name-of-a-number) – ARubiksCube Jun 18 '19 at 14:27

2 Answers2

2

This the solution to get the index from column header letter :-

public function letters2numbers($columnName) {
    $value = 0;
      $len = (strlen($columnName)-1);
    for ($i = 0; $i <= $len; $i++) {
       $delta = intval( ord($columnName[$i])  - 64);
       $value = intval($value*26)+ intval($delta);   
    } 
    return $value;
}
ADDC
  • 103
  • 7
0

This post is on an old question, but a correction is needed to keep from confusing future readers.

The examples show expected numbers from column references. However, column number for EC should be 133, not 132. I verified it's 133 with Excel's COLUMN function and with the function in the posted answer.