我知道如何从A到Z列出:
foreach (range('A', 'Z') as $char) {
echo $char . "n";
}
但是我如何从那里继续列出AA,AB,AC,AD,… AZ,BA,BB,BC等?
我做了一个快速的谷歌搜索,找不到任何东西,但我想这种方法会有所不同.
我想我可以通过使用for循环和内部字母的数组来做到这一点,尽管这种方式似乎有些粗鲁.
还有其他方法吗?
谢谢. 解决方法: PHP具有字符串增量运算符,它完全执行以下操作:
for($x = 'A'; $x < 'ZZ'; $x++)
echo $x, ' ';
结果:
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...
参考:
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 (编辑:北几岛)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|