Is it possible to get a range with PHP from A to ZZ*?
a b c ... aa ... zx zy zz
For me this didn\'t work:
range(\'A\', \'ZZ\');
I use alpha2num() to convert alpha to number and then use it in loop. With this I can get the range using any value for the start and end.
// to convert alpha to number
function alpha2num($a) {
$l = strlen($a);
$n = 0;
for($i = 0; $i < $l; $i++)
$n = $n*26 + ord($a[$i]) - 0x40;
return $n-1;
}
// to convert number back to alpha
function num2alpha($n)
{
for($r = ""; $n >= 0; $n = intval($n / 26) - 1)
$r = chr($n%26 + 0x41) . $r;
return $r;
}
function get_range($start_column, $end_column)
{
$s = alpha2num($start_column); // get start number
$e = alpha2num($end_column); // get end num
$columns = array();
// loop from start to end and change back the number to alpha to be stored in array
for($i=$s; $i<=$e; $i++)
$columns[] = num2alpha($i);
return $columns;
}
// usage
$columns = get_range('Z', 'BA'));