【问题标题】:PHP range() from A to ZZ?PHP range() 从 A 到 ZZ?
【发布时间】:2013-01-11 12:52:53
【问题描述】:

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

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

对我来说这不起作用:

range('A', 'ZZ');

它适用于 PHPExcel,当它给出 BE 作为最高字段时,我会遍历所有列。在这种情况下,我只得到 A,B:

range ('A', 'BE')

【问题讨论】:

标签: php range


【解决方案1】:

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

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

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

编辑

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

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

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

【讨论】:

  • 有趣的方法!我从来不知道你可以做一些奇怪的事情,比如增加字符串,但这似乎工作得很好:)
  • 循环中结束条件的主要问题是“不要使用 > 或
  • @MarkBaker 你知道我们不能使用递减运算符的原因吗?为什么“不要在循环中使用 > 或
  • Decrementor 根本没有为 alpha 编码,但不知道为什么它从来没有。为什么不 > 或
  • 想一想,我怀疑减量运算符没有为字符实现,因为当你减 'A' 或 'a' 时会发生什么......它不能简单地处理作为数字减量器
【解决方案2】:

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

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

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

【讨论】:

    【解决方案3】:
    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,
    }
    

    它正在工作

    【讨论】:

      【解决方案4】:

      您可以组合两个 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;
          }
      }
      

      【讨论】:

        【解决方案5】:

        使用内置的range 是不可能的:

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

        但是,本质上,您在这里所做的是从使用 26 位 a 的数字系统中的 1 向上计数到 z。因此,您可以通过计数、转换为基数 26(使用数字 09ap)然后将数字“转换”到范围 az.

        【讨论】:

          【解决方案6】:

          更好的选择(效果很好)

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

          【讨论】:

          • 它不会给出OP提到的'zz'完成值,你如何修改才能给出?
          • 使用 是危险的
          【解决方案7】:

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

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

          【讨论】:

            【解决方案8】:

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

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

            使用此递归函数获得从 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;
            }
            

            调用函数like.

            print_r(myRange('ZZ'));
            

            会给你结果

            一个 乙 C . . . 中兴 ZY ZZ

            【讨论】:

              【解决方案9】:

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

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

              【讨论】:

                【解决方案10】:

                这对我很有效。

                function create_columns_range($start = 'A', $end = 'ZZ'){
                    $return_range = [];
                    for ($i = $start; $i !== $end; $i++){
                        $return_range[] = $i;
                    }
                    return $return_range;
                }
                

                要使用它,只需调用:

                $range = create_columns_range('A', 'ZZ');
                print_r(range);
                

                【讨论】:

                  【解决方案11】:

                  这是我能帮到你的(生成从 A 到 Z 的数组)。

                  $a = range(65, 90);
                  array_walk($a, 'chr');
                  

                  查看chrarray_walk

                  【讨论】:

                  • OP 需要在 Z 之后进入 'AA', 'AB' , ...。此外,您的代码严格等同于 $a = range('A', 'Z')。哪个更简单,更清晰。
                  【解决方案12】:

                  我使用 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'));
                  

                  【讨论】:

                    【解决方案13】:
                    $atoz = range('A', 'Z');
                    $target = range('A', 'Z');
                    
                    $result = $atoz;
                    foreach ($target as $val) {
                        $step = array_map(function ($elem) use ($val) {
                                return $val. $elem;
                            }, $atoz);
                    
                        $result = array_merge($result, $step);
                    }
                    

                    【讨论】:

                    • 这个答案缺少教育解释。
                    【解决方案14】:
                    $abc = [];
                    foreach(range('A', 'Z') as $first) {
                        array_push($abc, $first);
                    }
                    
                    foreach(range('A', 'Z') as $second) {
                      foreach(range('A', 'Z') as $third) {      
                          array_push($abc, $second.$third);
                      }
                    }
                    
                    $headers = ['CAMPO', 'CAMPO', 'CAMPO', 'CAMPO', 'CAMPO', 'CAMPO', 'CAMPO', 'CAMPO', 'CAMPO', 'CAMPO', 'CAMPO', 'CAMPO', 'CAMPO', 'CAMPO', 'CAMPO', 'CAMPO', 'CAMPO', 'CAMPO', 'CAMPO', 'CAMPO', 'CAMPO', 'CAMPO', 'CAMPO', 'CAMPO', 'CAMPO', 'CAMPO', 'CAMPO', 'CAMPO', 'CAMPO'];
                    
                    foreach($headers as $key => $value) {
                      echo $abc[$key] . ' - ' . $value . PHP_EOL;
                    }
                    

                    【讨论】:

                    【解决方案15】:
                    $alfabet=["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","AC","AD","AE","AF","AG","AH","AI","AJ","AK","AL","AM","AN","AO","AP","AQ","AR","AS","AT","AU","AV","AW","AX","AY","AZ","BA","BB","BC","BD","BE","BF","BG","BH","BI","BJ","BK","BL","BM","BN","BO","BP","BQ","BR","BS","BT","BU","BV","BW","BX","BY","BZ","CA","CB","CC","CD","CE","CF","CG","CH","CI","CJ","CK","CL","CM","CN","CO","CP","CQ","CR","CS","CT","CU","CV","CW","CX","CY","CZ","DA","DB","DD","DD","DE","DF","DG","DH","DI","DJ","DK","DL","DM","DN","DO","DP","DQ","DR","DS","DT","DU","DV","DW","DX","DY","DZ"];
                    
                     $active_row_value_fulladdress = "AA - AM";
                     //$active_row_value_fulladdress = "A - F";
                     $taken_full = strtoupper($active_row_value_fulladdress);
                    
                    $taken_full_ar = explode("-", $taken_full);
                    $start = trim($taken_full_ar[0]);
                    $end   = trim($taken_full_ar[1]);
                    
                    $alfabet_key = array_flip($alfabet);
                    $start1 = $alfabet_key[$start];
                    $end1   = $alfabet_key[$end];
                    
                    for($i=$start1;$i<=$end1;$i++)
                    {
                        $taken_full_array[]=$alfabet[$i];
                    }
                    
                    foreach ($taken_full_array as $key => $value) 
                    {
                        $full_add[]=array_search($value,$alfabet);
                    }
                    
                    echo "<pre>";
                    print_r($taken_full_array);
                    print_r($full_add);
                    

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 1970-01-01
                      • 2015-01-13
                      • 1970-01-01
                      • 2023-04-05
                      • 1970-01-01
                      • 2022-01-13
                      • 1970-01-01
                      • 1970-01-01
                      相关资源
                      最近更新 更多