Created
May 26, 2017 20:00
-
-
Save alfredoem/2c6543354294094ce7428cde0fe08de8 to your computer and use it in GitHub Desktop.
Reduce length of a number
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 | |
namespace Aloha; | |
class NumberShorten | |
{ | |
// readable character set excluded (0,O,1,l) | |
// const CODESET = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
const CODESET = "23456789abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ"; | |
public function shorten($number){ | |
$base = strlen(self::CODESET); | |
$converted = ""; | |
while ($number > 0) { | |
$converted = substr(self::CODESET, ($number % $base), 1) . $converted; | |
$number = floor($number/$base); | |
} | |
return $converted; | |
} | |
function unshorten($shortened){ | |
$base = strlen(self::CODESET); | |
$c = 0; | |
for ($i = strlen($shortened); $i; $i--) { | |
$c += strpos(self::CODESET, substr($shortened, (-1 * ( $i - strlen($shortened) )),1)) * pow($base,$i-1); | |
} | |
return $c; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment