- var_dump: documentation ( source)
- strpos: documentation ( source)
- intval: documentation ( source)
- pow: documentation ( source)
<?php
class Foo{
public static $alphabet = 'ABCDEFGHIKLMNOPQRSTVXYZabcdefghiklmnopqrstvxyz';
public static function shorten($number) {
$alphabet = self::$alphabet;
$base = strlen($alphabet);
$number = intval($number, 10);
$shortened = "";
while ($number > 0) {
$remainder = $number % $base;
$shortened = $alphabet[$remainder] . $shortened;
$number = (int) ($number / $base);
}
return $shortened;
}
public static function unshorten($string) {
$alphabet = self::$alphabet;
$base = strlen($alphabet);
$length = strlen($string);
$returnVal = 0;
for ($i=$length-1; $i >= 0; --$i) {
$ind = strpos($alphabet, $string[$i]);
if ($ind === false) {
return false;
}
$returnVal += (int)($ind * pow($base, $i));
}
return $returnVal;
}
}
$shortened = Foo::shorten(12343336363636636);
echo $shortened . "\n";
var_dump(Foo::unshorten($shortened));