Repeat the alphabet similar to the excel columns

3

I'm creating a function that returns the alphabet in an array according to the size of passed columns.

At first I was only looping using the range ("A", "Z"); but when I had more than 26 columns I did not answer any more, what I found was a suggestion here link given by the answer from Willian Bruno, but that is also not very correct, at least it did not work here, hence using as a basis I created mine alone that also does not return the expected, which would be the alphabet (az, aa -zz, aaa-zzz), similar to the excel columns.

Follow what I'm doing.

function alphaRepeat($columnsSize = 10)
{
    $i = 0;
    $j = 0;

    $abc = range("A", "Z");
    $abcSize = count($abc);

    $alpha = array();

    while ($i < $columnsSize) {

        if (((int)($i / $abcSize) - $j) > 0) {
            $j++;
        }

        $item = '';

        if ($j > 0) {
            $item.= str_repeat($abc[$j - 1], $j);
        }
        $item.= $abc[$i - ($abcSize * $j)];

        $alpha[] = $item;

        $i++;
    }

    return $alpha;
}
    
asked by anonymous 30.10.2015 / 19:22

4 answers

4

It is possible to increment a string by its ASCII value in php since it is in the valid range of (65..90 A..Z, 97..122 a..z) so 'range' 'Z') works, you have more detailed explanation .

You can create a function to encapsulate this code, $maximo is the number entered by the user, $inicial can be a or A

Ideone example

<?php

$inicial = 'a';

$maximo = 27;
$atual = 0;

$colunas = array();
while($maximo > $atual){
    $colunas[] = $inicial++;
    $atual++;
}

Output:

Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => e
    [5] => f
    [6] => g
    [7] => h
    [8] => i
    [9] => j
    [10] => k
    [11] => l
    [12] => m
    [13] => n
    [14] => o
    [15] => p
    [16] => q
    [17] => r
    [18] => s
    [19] => t
    [20] => u
    [21] => v
    [22] => w
    [23] => x
    [24] => y
    [25] => z
    [26] => aa
)
    
30.10.2015 / 19:36
4

Here is a formula where you pass the column number, and receive the corresponding string. It can be used to obtain any column, without having to calculate the previous ones.

This is an interesting complement to @rray's response if you need to do multiple operations on non-consecutive columns, such as a spreadsheet update, or conversion of internal coordinates to coordinates for display (imagine showing a view of a spreadsheet, or a formula with internal references).

function CabecalhoPlanilha( $numero ) {
    $string = '';
    while( $numero ) {
        $string = chr( 65 /* 97 para minusculas */ + ( --$numero % 26 ) ) . $string;
        $numero = (int) ( $numero / 26 );
    }
    return $string;
}

Demo IDEONE

    
30.10.2015 / 21:22
2

You can also do this:

for ($a = 'a';  $a != 'aaa'; $a++) {
    $colunas[] = $a;
}

This will generate a array with a sequence of a to zz .

If you need to set a limit, you can do this:

function create_cell_range($limit) {

     for ($a = 'a', $current = 0; $current < $limit; $a++,$current++) 
     {
          $columns[] = $a;
     }

    return $columns;
}


var_dump(create_cell_range(3)); //array(3){'a', 'b', 'c'}

If you are using PHP versions greater than 5.5 , I do not recommend using array . In this case, it's best to use a Generator .

So:

 function create_cell_range($limit) {

     for ($a = 'a', $current = 0; $current < $limit; $a++,$current++) 
     {
          yield $current => $a;
     }

}

Then when you need it, you only need to use it in foreach .

So:

foreach(create_cell_range(27) as $key => $value) {
     echo "$key => $value";
}

Generators represent an absurd memory economy, since it does not create an item for each element of a array , but rather returns an item sequentially when needed.

    
30.10.2015 / 20:46
0

You can also do this:

function numberToColumnName($number){
    $abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $abc_len = strlen($abc);

    $result = "";
    $tmp = $number;

    while($number > $abc_len) {
        $remainder = $number % $abc_len;
        $result = $abc[$remainder-1].$result;
        $number = floor($number / $abc_len);
    }
    return $abc[$number-1].$result;
}

for($i = 0; $i < 200; $i++){
    echo numberToColumnName($i)."\n";
}

See working at Ideone

    
30.10.2015 / 20:55