PHP range() from A to ZZ?

后端 未结 12 1181
不思量自难忘°
不思量自难忘° 2020-11-30 06:52

Is it possible to get a range with PHP from A to ZZ*?

a b c ... aa ... zx zy zz

For me this didn\'t work:

range(\'A\', \'ZZ\');
12条回答
  •  没有蜡笔的小新
    2020-11-30 07:23

    It's not possible with the built-in range:

    Support for character sequences and decrementing arrays was added in 4.1.0. Character sequence values are limited to a length of one. If a length greater than one is entered, only the first character is used.

    However, in essence what you are doing here is counting upwards from 1 in a numeric system that uses the 26 digits a to z. So you can quickly hack together a solution by counting, converting to base 26 (which uses the digits 0 to 9 and a to p) and then "translating" the digits to the range a to z.

提交回复
热议问题