Created
June 9, 2022 14:34
-
-
Save jkoop/193b938ba72baf9a5abae2a93569008d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Converts a number to A,B,C,...,X,Y,Z,AA,AB,AC,...,ZZ,AAA,AAB, etc. | |
* | |
* @param int $int must be at least 1 | |
* @return string | |
*/ | |
function spreadsheetLetters(int $int): string { | |
if ($int < 0) throw new TypeError('$int must be at least 0'); | |
$numeric = $int % 26; | |
$letter = chr(65 + $numeric); | |
$int2 = intval($int / 26); | |
if ($int2 > 0) { | |
return (__FUNCTION__)($int2 - 1) . $letter; | |
} else { | |
return $letter; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment