Skip to content

Instantly share code, notes, and snippets.

@mbparvezme
Last active March 4, 2024 07:05
Show Gist options
  • Save mbparvezme/e7f7c9cc0b3532dc671409f660504c34 to your computer and use it in GitHub Desktop.
Save mbparvezme/e7f7c9cc0b3532dc671409f660504c34 to your computer and use it in GitHub Desktop.
<?php
// LINK
// https://kvz.io/create-short-ids-with-php-like-youtube-or-tinyurl.html
echo alphaID(8503056, TRUE);
function alphaID($in, $to_num = false, $pad_up = false, $pass_key = null)
{
$out = '';
$index = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$base = strlen($index);
if ($pass_key !== null) {
// Although this function's purpose is to just make the
// ID short - and not so much secure,
// with this patch by Simon Franz (https://blog.snaky.org/)
// you can optionally supply a password to make it harder
// to calculate the corresponding numeric ID
for ($n = 0; $n < strlen($index); $n++) {
$i[] = substr($index, $n, 1);
}
$pass_hash = hash('sha256',$pass_key);
$pass_hash = (strlen($pass_hash) < strlen($index) ? hash('sha512', $pass_key) : $pass_hash);
for ($n = 0; $n < strlen($index); $n++) {
$p[] = substr($pass_hash, $n, 1);
}
array_multisort($p, SORT_DESC, $i);
$index = implode($i);
}
if ($to_num) {
// Digital number <<-- alphabet letter code
$len = strlen($in) - 1;
for ($t = $len; $t >= 0; $t--) {
$bcp = bcpow($base, $len - $t);
$out = $out + strpos($index, substr($in, $t, 1)) * $bcp;
}
if (is_numeric($pad_up)) {
$pad_up--;
if ($pad_up > 0) {
$out -= pow($base, $pad_up);
}
}
} else {
// Digital number -->> alphabet letter code
if (is_numeric($pad_up)) {
$pad_up--;
if ($pad_up > 0) {
$in += pow($base, $pad_up);
}
}
for ($t = ($in != 0 ? floor(log($in, $base)) : 0); $t >= 0; $t--) {
$bcp = bcpow($base, $t);
$a = floor($in / $bcp) % $base;
$out = $out . substr($index, $a, 1);
$in = $in - ($a * $bcp);
}
}
return $out;
}
echo alphaID(9007199254740989);
// will return PpQXn7COf and:
echo alphaID('PpQXn7COf', true);
// will return 9007199254740989
?>
// LINK
// https://kvz.io/create-short-ids-with-php-like-youtube-or-tinyurl.html
/**
* Javascript AlphabeticID class
* (based on a script by Kevin van Zonneveld <kevin@vanzonneveld.net>)
*
* Author: Even Simon <even.simon@gmail.com>
*
* Description: Translates a numeric identifier into a short string and backwords.
*
* Usage:
* var str = AlphabeticID.encode(9007199254740989); // str = 'fE2XnNGpF'
* var id = AlphabeticID.decode('fE2XnNGpF'); // id = 9007199254740989;
**/
var AlphabeticID = {
index:'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ',
/**
* [@function](https://twitter.com/function) AlphabeticID.encode
* [@description](https://twitter.com/description) Encode a number into short string
* [@param](https://twitter.com/param) integer
* [@return](https://twitter.com/return) string
**/
encode:function(_number){
if('undefined' == typeof _number){
return null;
}
else if('number' != typeof(_number)){
throw new Error('Wrong parameter type');
}
var ret = '';
for(var i=Math.floor(Math.log(parseInt(_number))/Math.log(AlphabeticID.index.length));i>=0;i--){
ret = ret + AlphabeticID.index.substr((Math.floor(parseInt(_number) / AlphabeticID.bcpow(AlphabeticID.index.length, i)) % AlphabeticID.index.length),1);
}
return ret.reverse();
},
/**
* [@function](https://twitter.com/function) AlphabeticID.decode
* [@description](https://twitter.com/description) Decode a short string and return number
* [@param](https://twitter.com/param) string
* [@return](https://twitter.com/return) integer
**/
decode:function(_string){
if('undefined' == typeof _string){
return null;
}
else if('string' != typeof _string){
throw new Error('Wrong parameter type');
}
var str = _string.reverse();
var ret = 0;
for(var i=0;i<=(str.length - 1);i++){
ret = ret + AlphabeticID.index.indexOf(str.substr(i,1)) * (AlphabeticID.bcpow(AlphabeticID.index.length, (str.length - 1) - i));
}
return ret;
},
/**
* [@function](https://twitter.com/function) AlphabeticID.bcpow
* [@description](https://twitter.com/description) Raise _a to the power _b
* [@param](https://twitter.com/param) float _a
* [@param](https://twitter.com/param) integer _b
* [@return](https://twitter.com/return) string
**/
bcpow:function(_a, _b){
return Math.floor(Math.pow(parseFloat(_a), parseInt(_b)));
}
};
/**
* [@function](https://twitter.com/function) String.reverse
* [@description](https://twitter.com/description) Reverse a string
* [@return](https://twitter.com/return) string
**/
String.prototype.reverse = function(){
return this.split('').reverse().join('');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment