Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP range() from A to ZZ?

Tags:

php

range

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'); 

It's for PHPExcel, when it gives BE as highest field i'd run through all colums. In this case i only get A, B:

range ('A', 'BE') 
like image 292
Ronn0 Avatar asked Jan 11 '13 12:01

Ronn0


People also ask

What is purpose of range() in PHP?

The range() function creates an array containing a range of elements. This function returns an array of elements from low to high. Note: If the low parameter is higher than the high parameter, the range array will be from high to low.

How do I view an array in PHP?

To display array structure and values in PHP, we can use two functions. We can use var_dump() or print_r() to display the values of an array in human-readable format or to see the output value of the program array.

Is array an element in PHP?

The in_array() function is an inbuilt function in PHP that is used to check whether a given value exists in an array or not. It returns TRUE if the given value is found in the given array, and FALSE otherwise.


1 Answers

Take advantage of PHP's ability to increment characters "perl-style"

$letters = array(); $letter = 'A'; while ($letter !== 'AAA') {     $letters[] = $letter++; } 

But you could also use simple integer values, and take advantage of PHPExcel's built-in PHPExcel_Cell::stringFromColumnIndex() method

EDIT

From PHP 5.5, you can also use Generators to avoid actually building the array in memory

function excelColumnRange($lower, $upper) {     ++$upper;     for ($i = $lower; $i !== $upper; ++$i) {         yield $i;     } }  foreach (excelColumnRange('A', 'ZZ') as $value) {     echo $value, PHP_EOL; } 
like image 90
Mark Baker Avatar answered Oct 05 '22 20:10

Mark Baker