Вопрос
I know that in PHP you can declare a variable A and then if you increment it in a loop it will go A,B,C,....X,Y,Z,AA,AB,AC
Is there any php function to convert an integer to the corresponding string in that format
Решение
I started with Tor P's answer and I think this works as desired:
function numToString($i) {
if($i < 26) {
return chr(65 + $i);
} else {
$i -= 26;
$r = $i % 26;
return numToString($i - $r).chr(65 + $r);
}
}
Другие советы
The closest thing to that format is base_convert, but it uses the 0-9A-Z charset.
You can use base 26, and then shift the charset.
use the chr function with the int transformed into an ascii code (capital letters go from 65-90), so 65+i, to use i >= 26 you have to do one letter at a time
so, something like this...
function noToString($i) {
if($i < 26) return chr(65+$i);
else return noToString($i-26).chr(65+$i%26);
}
Не связан с StackOverflow