Created
September 23, 2016 08:52
-
-
Save Grebenschikov/2807fc365763d9f3431d5baa3e8c5174 to your computer and use it in GitHub Desktop.
Convert int to arbitrary base with custom alphabet
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
<?php | |
echo base_encode('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 100500) . PHP_EOL; | |
function base_encode($alphabet, $number, $suffix = '') { | |
$base = strlen($alphabet); | |
if ($number >= $base) { | |
$div = intval($number / $base); // For php7: intdiv($number, $base); | |
$mod = $number % $base; | |
return base_encode($alphabet, $div, $alphabet[$mod] . $suffix); | |
} else { | |
return $alphabet[$number] . $suffix; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment