Skip to content

Instantly share code, notes, and snippets.

@MayMeow
Last active January 15, 2022 08:16
Show Gist options
  • Save MayMeow/bbf64c8428cad1862ba5 to your computer and use it in GitHub Desktop.
Save MayMeow/bbf64c8428cad1862ba5 to your computer and use it in GitHub Desktop.
I found this on internet when i need something to shorten link... This is class but its not problem to create cakephp component... (i can post this one too if anyone want it)
<?php
class Converter {
public function convert ($number = null) {
$codeset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$base = strlen($codeset);
$n = $number;
$converted = "";
while ($n > 0) {
$converted = substr($codeset, ($n % $base), 1) . $converted;
$n = floor($n/$base);
}
return $converted; //return converted value
}
public function deconvert ($conv = null) {
$codeset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$base = strlen($codeset);
$converted = $conv;
$c = 0;
for ($i = strlen($converted); $i; $i--) {
$c += strpos($codeset, substr($converted, (-1 * ( $i - strlen($converted) )),1))
* pow($base,$i-1);
}
return $c;
}
}
$converter = new Converter();
// convert number
echo $converter::convert(15000007); // returns 10WbB
echo $converter::deconvert('10WbB'); // will return 15000007
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment