Skip to content

Instantly share code, notes, and snippets.

@alfredoem
Created May 26, 2017 20:00
Show Gist options
  • Save alfredoem/2c6543354294094ce7428cde0fe08de8 to your computer and use it in GitHub Desktop.
Save alfredoem/2c6543354294094ce7428cde0fe08de8 to your computer and use it in GitHub Desktop.
Reduce length of a number
<?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