48

I need to print/list alphabetical(A-Z) chararcters to manage Excel cells. Is there any PHP function to list alphabetic?

I need result as

A1
B1
C1
D1
...
...

OR

A
B
C
...
...

Is this possible?

Martijn
  • 15,791
  • 4
  • 36
  • 68
Prabhu M
  • 2,852
  • 8
  • 37
  • 54

11 Answers11

133

You can either do:

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

Or:

for ($char = 'A'; $char <= 'Z'; $char++) {
    echo $char . "\n";
}
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
15

range() supports letters since PHP 4.1, so you can do this:

$azRange = range('A', 'Z');
foreach ($azRange as $letter)
{
  print("$letter\n");
}
John Carter
  • 53,924
  • 26
  • 111
  • 144
6

You can do it in this way as well: ASCII range used here.

for($i = 65 ; $i<=90; $i++)
{
  echo chr($i);
}
valar morghulis
  • 2,007
  • 27
  • 34
3

I made a constant time function as follows

This function gives the Alphabetic representation of a numeric index

public static $alpha = ['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'];

public static function getColName($index){
  $index--;
  $nAlphabets = 26;
  $f = floor($index/pow($nAlphabets,0)) % $nAlphabets;
  $s = (floor($index/pow($nAlphabets,1)) % $nAlphabets)-1;
  $t = (floor($index/pow($nAlphabets,2)) % $nAlphabets)-1;

  $f = $f < 0 ? '' : self::$alpha[$f];
  $s = $s < 0 ? '' : self::$alpha[$s];
  $t = $t < 0 ? '' : self::$alpha[$t];

  return trim("{$t}{$s}{$f}");

}

Now if you want to use it create a range. you can call this function in a loop pushing your values to an array.

As for most of the time, we need the representation rather than a range this function would work just fine.

HOW TO USE

Just enclose these static functions in a class and use it as

className::getColName(47);

Making a range in my case was a waste of memory.

Abhijit Srivastava
  • 1,419
  • 2
  • 10
  • 33
  • It is not a waste of processor however. And if it has a technical merit that is escaping me, it comes with the flaw of being unreadable, and more importantly, it has the inconvenient of not corresponding to the requirements set by the topic. Unless there are excel cells that correspond to an empty string or a combination of two letters. But those that have two letters also require a number for rows. – Bevelopper Oct 20 '20 at 08:57
2

here's my code for HTML using P.H.P code, and it works fine

for($i = 'a' ; $i <= 'z' ; $i++){
echo $i. "<br />"; }
2

I think you should use the range function:

$a=range("A","Z");
foreach($a as $char)
    echo $char."\n";
mck89
  • 18,918
  • 16
  • 89
  • 106
2

This:

$range = range("A", "Z");
for ($i=1; i<=100; i++) {
    foreach ($range as $letter) {
      print("$letter$i\n");
    }
}

will print you all the combinations:
A1
B1
C1
... ...
... ...
V100
W100
Z100

Modify the ranges accordingly to your needs.

Dom De Felice
  • 476
  • 4
  • 10
1

this code results your requirements....

<?php 
        $x= 'A';
            for($i=0;$i<26;$i++)
            {
                echo $x."<br/>";//generates A,B,C,D...Z
                $x++;
                if($i == 25)
                {
                    $x = 'A';
                    $y = '1';
                    for($j=0;$j<26;$j++)
                    {
                        echo $x.$y."<br />";//generates A1,B1...Z1
                        $x++;
                        if($j == 25)
                        { 
                            $x = 'A';
                            $y++;
                            for($k=0;$k<26;$k++)
                            {
                               echo $x.$y."<br />";//generates A2,B2....Z2
                               $x++; 
                            }
                        }
                    }
                }
            }
        ?>
Kamal Lama
  • 690
  • 2
  • 5
  • 20
1
$len = 0;
for ($char = 'A'; $char <= 'Z'; $char++) {
    $len++;
    if ($len == 26) {
        break;
    }
    echo $char;
}
  • 1
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – andreas Oct 31 '16 at 09:32
  • **Flaggers / reviewers:** [For code-only answers such as this one, downvote, don't delete!](//meta.stackoverflow.com/a/260413/2747593) – Scott Weldon Oct 31 '16 at 14:34
0

If you're looking for a comprehensive set of Excel functionality, then take a look at PHPExcel which provides a lot of methods for manipulating cell addresses and ranges, as well as reading/writing for Excel and various other spreadsheet file formats

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
0

This code for display A,B,...,ZY,ZZ.

function print_char($end_no=90, $start_no=65, $prefix_no=0)
{ $vasant='';
    if($prefix_no==0)
    {
        for($set=$start_no; $set<=$end_no; $set++)
        {
            $vasant.=(chr($prefix_no).chr($set)).', ';
        }
    }
    else
    {
        for($set=$start_no; $set<=90; $set++)
        {
            $vasant.=(chr($set)).', ';
        }
        for($pre_loop=65; $pre_loop<=$prefix_no; $pre_loop++)
        {
            for($set=$start_no; $set<=90; $set++)
            {
                if($set>=$end_no && $pre_loop==$prefix_no)
                {
                    $vasant.=(chr($pre_loop).chr($set)).'. ';
                    break;
                }
                else
                {
                    $vasant.=(chr($pre_loop).chr($set)).', ';
                }
            }
        }
    }
    return $vasant;
}


$show=print_char(90,65,90);
echo $show;