PHP range() from A to ZZ?
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
How to list from A to Z in PHP, and then on to AA, AB, AC, etc
PHP has the string increment operator that does exactly that:
for($x = 'A'; $x < 'ZZ'; $x++)
echo $x, ' ';
Result:
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...
Ref:
PHP follows Perl's convention when dealing with arithmetic operations on character variables and not C's. For example, in PHP and Perl $a = 'Z'; $a++; turns $a into 'AA', while in C a = 'Z'; a++; turns a into '[' (ASCII value of 'Z' is 90, ASCII value of '[' is 91). Note that character variables can be incremented but not decremented and even so only plain ASCII alphabets and digits (a-z, A-Z and 0-9) are supported. Incrementing/decrementing other character variables has no effect, the original string is unchanged.
http://php.net/manual/en/language.operators.increment.php
In PHP why does range a-Z and a for loop applied to the alphabet produce different characters?
Just read the manual: http://php.net/manual/en/language.operators.increment.php
PHP follows Perl's convention when dealing with arithmetic operations
on character variables and not C's. For example, in PHP and Perl $a =
'Z'; $a++; turns $a into 'AA', while in C a = 'Z'; a++; turns a into
'[' (ASCII value of 'Z' is 90, ASCII value of '[' is 91). Note that
character variables can be incremented but not decremented and even so
only plain ASCII alphabets and digits (a-z, A-Z and 0-9) are
supported. Incrementing/decrementing other character variables has no
effect, the original string is unchanged.
php: How to list from a to z, aa to zz, aA to aZ, and so on
function generate($length = 1) {
$small = range('a', 'z');
$capital = range('A', 'Z');
if ($length <= 0) {
return [];
} elseif ($length == 1) {
return array_merge($small, $capital);
} else {
$result = [];
foreach (array_merge($small, $capital) as $letter) {
foreach (generate($length - 1) as $subLetter) {
$result[] = $letter . $subLetter;
}
}
return $result;
}
}
// Sample output
echo '<pre>' . print_r(generate(2), true) . '</pre>';
Try it online
Array with multiple numerical ranges
$alphas = array_merge(range('A', 'Z'), range('a', 'z'));
refer
Way to get all alphabetic chars in an array in PHP?
Is there a range('a','z') for Non-English Alphabet
$arr = range('а', 'я');
var_dump($arr);
But this will work with cp1251
only and lost ё
, since it is in the end of the ascii table
PHP: Define a variable within two or more ranges with alternatives to array_merge()?
You can merge the original range()
with the new range, and assign it back to $x
. You do not have to give it a new name for every new value it gets, you can overwrite the value instead.
$x = range(0,10);
$x = array_merge($x, range(15,30));
print_r($x);
Range php for chr(33, 47)
$other = array_map('chr',range(33,47));
Related Topics
Sending Multiple Data Parameters with Jquery Ajax
Mock in PHPunit - Multiple Configuration of the Same Method with Different Arguments
Why Are PHP Function Calls *So* Expensive
Codeigniter Multiple File Upload Messes File Extension
Adding Three Months to a Date in PHP
Stop Script Execution Upon Notice/Warning
Can Servers Block Curl Requests
Fatal Error: Out of Memory, But I Do Have Plenty of Memory (Php)
How to Convert These Strange Characters? (ë, Ã, ì, ù, Ã)
Calculate the Number of Months Between Two Dates in PHP
Looping Through Query Results Multiple Times with MySQLi_Fetch_Array
Change Date Format (In Db or Output) to Dd/Mm/Yyyy - PHP MySQL