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

I know how to list from A to Z:

foreach (range('A', 'Z') as $char) { echo $char . "\n"; } 

But how do I go from here to the list of AA, AB, AC, AD, ... AZ, BA, BB, BC, etc.?

I quickly searched Google and found nothing, although I think the approach will be different.

I think I can do this using a for loop and an array with the letters inside, although this way seems a bit crude.

Any other way?

Thanks.

+2
source share
4 answers

PHP has a line increment operator that does just that:

 for($x = 'A'; $x < 'ZZ'; $x++) echo $x, ' '; 

Result:

 ABCDEFGHIJKLMNOPQRSTU VWXYZ AA AB AC AD AE AF... 

Ref:

PHP follows the Perl convention when working with arithmetic operations on character variables, not C. For example, in PHP and Perl $ a = 'Z'; $ A ++; turns $ a into 'AA', and into C a = 'Z'; A ++; turns a into '[' (ASCII 'Z' value is 90, ASCII '[' value is 91). Please note that character variables can increase, but not decrease, and therefore only simple alphabets and ASCII numbers (az, AZ and 0-9) are supported. The increment / decrement of other character variables is not affected, the original string is not changed.

http://php.net/manual/en/language.operators.increment.php

+10
source

Try

 foreach (range('A', 'Z') as $char) { foreach (range('A', 'Z') as $char1) { echo $char . $char1. "\n"; } } 
+2
source

I messed up a bit and got the following:

 $addon = 64; for ($i = 1; $i <= 700; $i++) { $prefix = ""; if ($i > 26) { $remainder = floor($i / 26); $prefix = chr($remainder + $addon); } $ivalue = ($i % 26 == 0) ? 26 : $i % 26; echo $prefix . chr($addon + $ivalue) . "<br />"; } 

$addon is 64, because the character code for A is 65, which means we just add it to go from A to Z. Works up to ZZ - feel free to work for AAA, AAB, AAC, etc.

0
source

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

You can use this for excel column sheet as well

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

will give you the result

a B C,,, A.A. AB AC

0
source

Source: https://habr.com/ru/post/984452/


All Articles