fork(4) download
  1. <?php
  2.  
  3. function column_id_to_int($columnId) {
  4. if (!$columnId)
  5. throw new DomainException("Column-id cannot be empty.");
  6. $columnId = strtoupper($columnId);
  7. if ( !preg_match('~[A-Z]+~',$columnId) )
  8. throw new DomainException("String '$columnId' is not a valid column-id.");
  9. $sum = 0;
  10. for ($x=0; $x<strlen($columnId); $x++ ) {
  11. $sum = $sum*26;
  12. $sum += (ord($columnId[$x]) - 65 + 1);
  13. }
  14. return $sum;
  15. }
  16.  
  17. print 'AA <=> ' . column_id_to_int('AA') . "\n";
  18. print 'ABC <=> ' . column_id_to_int('ABC') . "\n";
  19.  
  20. ?>
Success #stdin #stdout 0.03s 52432KB
stdin
Standard input is empty
stdout
AA <=> 27
ABC <=> 731