PHP range() 从 A 到 ZZ?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14278603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:59:33  来源:igfitidea点击:

PHP range() from A to ZZ?

phprange

提问by Ronn0

Is it possible to get a range with PHP from A to ZZ*?

是否可以使用 PHP 获得从 A 到 ZZ* 的范围?

a b c ... aa ... zx zy zz

abc ... aa ... zx zy zz

For me this didn't work:

对我来说这不起作用:

range('A', 'ZZ');

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

它是针对 PHPExcel 的,当它给出BE作为我将遍历所有列的最高字段时。在这种情况下,我只得到 A、B:

range ('A', 'BE')

采纳答案by Suresh Kamrushi

Just Try this- (tested working fine)

试试这个 - (测试工作正常)

function createColumnsArray($end_column, $first_letters = '')
{
  $columns = array();
  $length = strlen($end_column);
  $letters = range('A', 'Z');

  // Iterate over 26 letters.
  foreach ($letters as $letter) {
      // Paste the $first_letters before the next.
      $column = $first_letters . $letter;

      // Add the column to the final array.
      $columns[] = $column;

      // If it was the end column that was added, return the columns.
      if ($column == $end_column)
          return $columns;
  }

  // Add the column children.
  foreach ($columns as $column) {
      // Don't itterate if the $end_column was already set in a previous itteration.
      // Stop iterating if you've reached the maximum character length.
      if (!in_array($end_column, $columns) && strlen($column) < $length) {
          $new_columns = createColumnsArray($end_column, $column);
          // Merge the new columns which were created with the final columns array.
          $columns = array_merge($columns, $new_columns);
      }
  }

  return $columns;
}
echo "<pre>";
print_r( createColumnsArray('BZ'));

copied from http://php.net/range

复制自http://php.net/range

回答by Mark Baker

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

利用 PHP 增加字符“perl 风格”的能力

$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

但您也可以使用简单的整数值,并利用 PHPExcel 的内置 PHPExcel_Cell::stringFromColumnIndex() 方法

EDIT

编辑

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

从 PHP 5.5 开始,您还可以使用 Generators 来避免在内存中实际构建数组

function excelColumnRange($lower, $upper) {
    ++$upper;
    for ($i = $lower; $i !== $upper; ++$i) {
        yield $i;
    }
}

foreach (excelColumnRange('A', 'ZZ') as $value) {
    echo $value, PHP_EOL;
}

回答by Donot Don't

for ($i = 'A'; $i !== 'AC'; $i++){
    echo $i.', '; //A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, AA, AB,
}

it's working

它在工作

回答by Oldskool

You can combine two foreach loops to generate something like that.

您可以组合两个 foreach 循环来生成类似的内容。

// Single letters
foreach(range('A', 'Z') as $letter) {
    echo $letter;
}

// AA-ZZ combinations
foreach(range('A', 'Z') as $letter1) {
    foreach(range('A', 'Z') as $letter2) {
        echo $letter1 . $letter2;
    }
}

回答by Suresh Kamrushi

Even better option (Working great)

更好的选择(效果很好)

for ($i = 'a'; $i < 'zz'; $i++) 
    echo $i."<br>";

回答by Jon

It's not possible with the built-in range:

内置是不可能的range

Support for character sequences and decrementing arrays was added in 4.1.0. Character sequence values are limited to a length of one. If a length greater than one is entered, only the first character is used.

在 4.1.0 中添加了对字符序列和递减数组的支持。字符序列值的长度限制为 1。如果输入的长度大于 1,则仅使用第一个字符。

However, in essence what you are doing here is counting upwards from 1 in a numeric system that uses the 26 digits ato z. So you can quickly hack together a solution by counting, converting to base 26 (which uses the digits 0to 9and ato p) and then "translating" the digits to the range ato z.

但是,本质上,您在这里所做的是在使用 26 位数字的数字系统中从 1 向上计数az。因此,您可以通过计数、转换为基数 26(使用数字0to9ato p)然后将数字“转换”到范围ato来快速组合解决方案z

回答by Tarilo

You could ofcourse write your own function to do this as it seems that the range() function in php doesn't support this. This should be an easy job, since you can just nest the range function in another loop. Something like this:

您当然可以编写自己的函数来执行此操作,因为 php 中的 range() 函数似乎不支持此功能。这应该是一项简单的工作,因为您可以将 range 函数嵌套在另一个循环中。像这样的东西:

foreach(range('a', 'z') as $outer) {
  foreach(range('a', 'z') as $inner) {
    print($outer.$inner);
  }
}

回答by Shailesh Sonare

Please check this simple solution incrementing char.

请检查这个简单的解决方案增加字符。

How to list from A to Z in PHP, and then on to AA, AB, AC, etc

如何在 PHP 中从 A 到 Z 列出,然后再到 AA、AB、AC 等

Use this recursive function to get the exact range from A to ZZ

使用这个递归函数来获得从 A 到 ZZ 的精确范围

function myRange($end_column = '', $first_letters = '') {
    $columns = array();
    $length = strlen($end_column);
    $letters = range('A', 'Z');

    // Iterate over 26 letters.
    foreach ($letters as $letter) {
      // Paste the $first_letters before the next.
      $column = $first_letters . $letter; 
      // Add the column to the final array.
      $columns[] = $column;
      // If it was the end column that was added, return the columns.
      if ($column == $end_column)
          return $columns;
    }

    // Add the column children.
    foreach ($columns as $column) {
      // Don't itterate if the $end_column was already set in a previous itteration.
      // Stop iterating if you've reached the maximum character length.
      if (!in_array($end_column, $columns) && strlen($column) < $length) {
          $new_columns = myRange($end_column, $column);
          // Merge the new columns which were created with the final columns array.
          $columns = array_merge($columns, $new_columns);
      }
    }

    return $columns;
}

call function like.

调用函数之类的。

print_r(myRange('ZZ'));

will give you result

会给你结果

A B C . . . ZX ZY ZZ

乙丙。. . ZX ZY ZZ

回答by Mayur Chauhan

Here is a simplified solution in which you can define number of rows and columns you want to generate. This way you will allocate lesser memory.

这是一个简化的解决方案,您可以在其中定义要生成的行数和列数。这样你将分配更少的内存。

// Get cell names for excel
function generate_excel_cell_names($row_cnt, $col_cnt){
    $excel_cells = [];

    // Note: Row and col indexes are starting from 1
    for ($excel_row=1; $excel_row <= $row_cnt; $excel_row++) { 
        $excel_col = 'A';
        for ($col_index = 1; $col_index <= $col_cnt; $col_index++)
        {
            $excel_cells[$excel_row][$col_index] = $excel_col.$excel_row;
            $excel_col++;
        }
    }
    return $excel_cells;
}

回答by Zan Asmon

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.

我使用 alpha2num() 将 alpha 转换为数字,然后在循环中使用它。有了这个,我可以使用开始和结束的任何值来获取范围。

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