Skip to content

Instantly share code, notes, and snippets.

@GRMrGecko
Last active August 29, 2015 14:25
Show Gist options
  • Save GRMrGecko/b9eb10e8bf7aa003eaa0 to your computer and use it in GitHub Desktop.
Save GRMrGecko/b9eb10e8bf7aa003eaa0 to your computer and use it in GitHub Desktop.
Sort ID generator for things such as short URLs using the Bijection algorithm.
<?
/*
Copyright (c) 2015 Mr. Gecko's Media (James Coleman). http://mrgeckosmedia.com/
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
class shortID {
var $lowerCase = false;
var $check = array(3, 20);
var $charactersAllCase = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_.";
var $charactersLowerCase = "abcdefghijklmnopqrstuvwxyz1234567890-_.";
var $alphabet = array();
var $base = 0;
function __construct($lowerCase = false) {
$this->lowerCase = $lowerCase;
$this->alphabet = str_split(($this->lowerCase ? $this->charactersLowerCase : $this->charactersAllCase));
$this->base = count($this->alphabet);
}
function shuffleAlphabet($lowerCase = false) {
$alphabet = str_split(($lowerCase ? $this->charactersLowerCase : $this->charactersAllCase));
$count = count($alphabet);
for ($i=0; $i<60; $i++) {
for ($c=0; $c<$count; $c++) {
$newPos = rand(0, $count);
$tmp = $alphabet[$c];
$alphabet[$c] = $alphabet[$newPos];
$alphabet[$newPos] = $tmp;
}
}
return implode($alphabet);
}
function encode($id) {
if ($id<=0) {
return "";
}
$checkString = "";
foreach ($this->check as $check) {
$checkString .= $this->alphabet[$id%$check];
}
$encoded = "";
while ($id>0) {
$encoded = $this->alphabet[$id%$this->base].$encoded;
$id = (int)($id/$this->base);
}
return $checkString.$encoded;
}
function decode($encoded) {
$checkSize = count($this->check);
if (strlen($encoded)<=$checkSize) {
return 0;
}
$id = 0;
$checkString = substr($encoded, 0, $checkSize);
$values = str_split(substr($encoded, $checkSize));
foreach ($values as $value) {
$id = ($id*$this->base)+array_search($value, $this->alphabet);
}
$newCheckString = "";
foreach ($this->check as $check) {
$newCheckString .= $this->alphabet[$id%$check];
}
if ($newCheckString!=$checkString) {
return 0;
}
return $id;
}
}
$id = 20000;
$shortID = new shortID();
$encoded = $shortID->encode($id);
echo "Encoded: ".$encoded."\n";
$decoded = $shortID->decode($encoded);
echo "Decoded: ".$decoded."\n";
echo "Randomized: ".$shortID->shuffleAlphabet(false)."\n";
echo "Randomized lower case: ".$shortID->shuffleAlphabet(true)."\n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment