Last active
December 25, 2015 00:59
-
-
Save mcrumm/6891237 to your computer and use it in GitHub Desktop.
URL shortening function from https://github.com/briancray/PHP-URL-Shortener
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 | |
/** | |
* Generate a short URL based on an ID | |
* | |
* This function was lifted from: https://github.com/briancray/PHP-URL-Shortener | |
* | |
* @param integer $integer The ID from which to generate the short URL | |
* @param string $base A string containing the set of characters used to construct the short URL | |
* | |
* @return string The short URL | |
*/ | |
function shortURL($integer, $base = '23456789abcdefghjkmnpqrstvwxyz') { | |
$out = ''; | |
$length = strlen($base); | |
$base = str_split($base); | |
while ($integer > $length - 1) { | |
$out = $base[fmod($integer, $length)] . $out; | |
$integer = floor( $integer / $length ); | |
} | |
return $base[$integer] . $out; | |
} | |
$ids = array(1, 19, 287, 2076, 33043, 500032, 1786349); | |
foreach ($ids as $id) { | |
printf('%d: %s' . PHP_EOL, $id, shortURL($id)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment