PHP range() from A to ZZ?

后端 未结 12 1161
不思量自难忘°
不思量自难忘° 2020-11-30 06:52

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\');
12条回答
  •  独厮守ぢ
    2020-11-30 07:14

    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;
    }
    

提交回复
热议问题