Skip to content

Instantly share code, notes, and snippets.

@mcrumm
Last active December 25, 2015 00:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcrumm/6891237 to your computer and use it in GitHub Desktop.
Save mcrumm/6891237 to your computer and use it in GitHub Desktop.
URL shortening function from https://github.com/briancray/PHP-URL-Shortener
<?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