Skip to content

Instantly share code, notes, and snippets.

@jkoop
Created June 9, 2022 14:34
Show Gist options
  • Save jkoop/193b938ba72baf9a5abae2a93569008d to your computer and use it in GitHub Desktop.
Save jkoop/193b938ba72baf9a5abae2a93569008d to your computer and use it in GitHub Desktop.
/**
* 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