使用PHP列出字母(A-Z)的最佳方法

时间:2021-04-01 03:51:51

can any one please let me know, i need to print/list Alphabetical(A-Z) char to manage Excel cells. Is there any php function to list Alphabetic?

任何人请告诉我,我需要打印/列出字母(A-Z)字符来管理Excel单元格。是否有任何PHP功能列出字母?

I need result as

我需要结果

A1
B1
C1
D1
...
...
...

OR

要么

A
B
C
...
...

11 个解决方案

#1


96  

You can either do:

你可以这样做:

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

Or:

要么:

for ($char = 'A'; $char <= 'Z'; $char++) {
    echo $char . "\n";
}

#2


12  

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

range()支持自PHP 4.1以来的字母,所以你可以这样做:

$azRange = range('A', 'Z');
foreach ($azRange as $letter)
{
  print("$letter\n");
}

#3


2  

I think you should use the range function:

我认为你应该使用范围功能:

$a=range("A","Z");
foreach($a as $char)
    echo $char."\n";

#4


2  

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

这是我使用P.H.P代码的HTML代码,它工作正常

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

#5


1  

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

将打印所有组合:A1 B1 C1 ...... ...... ...... V100 W100 Z100

Modify the ranges accordingly to your needs.

根据您的需要修改范围。

#6


1  

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

你也可以这样做:这里使用的是ASCII范围。

for($i = 65 ; $i<=90; $i++)
{
  echo chr($i);
}

#7


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++; 
                            }
                        }
                    }
                }
            }
        ?>

#8


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

如果您正在寻找一套全面的Excel功能,那么请查看PHPExcel,它提供了许多操作单元格地址和范围的方法,以及Excel和其他各种电子表格文件格式的读/写

#9


0  

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

此代码用于显示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;

#10


0  

$len = 0;
for ($char = 'A'; $char <= 'Z'; $char++) {
    $len++;
    if ($len == 26) {
        break;
    }
    echo $char;
}

#11


0  

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.

在我的案例中制作一个范围是浪费记忆。

#1


96  

You can either do:

你可以这样做:

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

Or:

要么:

for ($char = 'A'; $char <= 'Z'; $char++) {
    echo $char . "\n";
}

#2


12  

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

range()支持自PHP 4.1以来的字母,所以你可以这样做:

$azRange = range('A', 'Z');
foreach ($azRange as $letter)
{
  print("$letter\n");
}

#3


2  

I think you should use the range function:

我认为你应该使用范围功能:

$a=range("A","Z");
foreach($a as $char)
    echo $char."\n";

#4


2  

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

这是我使用P.H.P代码的HTML代码,它工作正常

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

#5


1  

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

将打印所有组合:A1 B1 C1 ...... ...... ...... V100 W100 Z100

Modify the ranges accordingly to your needs.

根据您的需要修改范围。

#6


1  

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

你也可以这样做:这里使用的是ASCII范围。

for($i = 65 ; $i<=90; $i++)
{
  echo chr($i);
}

#7


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++; 
                            }
                        }
                    }
                }
            }
        ?>

#8


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

如果您正在寻找一套全面的Excel功能,那么请查看PHPExcel,它提供了许多操作单元格地址和范围的方法,以及Excel和其他各种电子表格文件格式的读/写

#9


0  

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

此代码用于显示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;

#10


0  

$len = 0;
for ($char = 'A'; $char <= 'Z'; $char++) {
    $len++;
    if ($len == 26) {
        break;
    }
    echo $char;
}

#11


0  

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.

在我的案例中制作一个范围是浪费记忆。