Professional Documents
Culture Documents
Create Youtube
Create Youtube
Create Youtube
PHP/Python/Javascript/Java/SQL
IDs are often numbers. Unfortunately there are only 10 digits to work with, so if you have a lot
of records, IDs tend to get very lengthy. For computers that's OK. But human beings like their
IDs as short as possible. So how can we make IDs shorter? Well, we could borrow characters
from the alphabet as have them pose as additional numbers. Alphabet to the rescue!
Other title options where
How to create unique short string IDs with PHP & MySQL
To do the conversion I've written a PHP function that can translate big numbers to short strings
and vice versa. I call it: alphaID.
The resulting string is not hard to decipher, but it can be a very nice feature to make URLs or
directory structures more compact and significant.
So basically:
Source
<?php
/**
* Translates a number to a short alhanumeric version
*
* Translated any number up to 9007199254740992
* to a shorter version in letters e.g.:
* 9007199254740989 --> PpQXn7COf
*
* specifiying the second argument true, it will
* translate back e.g.:
* PpQXn7COf --> 9007199254740989
*
*
* if ($number_in != $number_out) {
* echo "Conversion failure, ".$alpha_in." returns ".$number_out." instead of the ";
* echo "desired: ".$number_in."\n";
*}
* if ($alpha_in != $alpha_out) {
* echo "Conversion failure, ".$number_in." returns ".$alpha_out." instead of the ";
* echo "desired: ".$alpha_in."\n";
*}
*
* // Show //
* echo $number_out." => ".$alpha_out."\n";
* echo $alpha_in." => ".$number_out."\n";
* echo alphaID(238328, false)." => ".alphaID(alphaID(238328, false), true)."\n";
*
* // expects:
* // 2188847690240 => SpQXn7Cb
* // SpQXn7Cb => 2188847690240
* // aaab => 238328
*
* </code>
*
* @author Kevin van Zonneveld <kevin@vanzonneveld.net>
* @author Simon Franz
* @author Deadfish
* @author SK83RJOSH
* @copyright 2008 Kevin van Zonneveld (http://kevin.vanzonneveld.net)
* @license http://www.opensource.org/licenses/bsd-license.php New BSD Licence
* @version SVN: Release: $Id: alphaID.inc.php 344 2009-06-10 17:43:59Z kevin $
* @link
http://kevin.vanzonneveld.net/
*
* @param mixed $in String or long input to translate
* @param boolean $to_num Reverses translation when true
* @param mixed $pad_up Number or boolean padds the result up to a specified length
* @param string $pass_key Supplying a password makes it harder to calculate the original
ID
*
* @return mixed string or long
*/
function alphaID($in, $to_num = false, $pad_up = false, $pass_key = null)
{
$out = '';
$index = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$base = strlen($index);
$pass_hash = hash('sha256',$pass_key);
$pass_hash = (strlen($pass_hash) < strlen($index) ? hash('sha512', $pass_key) :
$pass_hash);
if ($to_num) {
// Digital number <<-- alphabet letter code
$len = strlen($in) - 1;
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);
}
}
return $out;
}
Example
Running:
alphaID(9007199254740989);
alphaID('PpQXn7COf', true);
More Features
There also is an optional third argument: $pad_up. This enables you to make the resulting
alphaID at least X characters long.
You can support even more characters (making the resulting alphaID even smaller) by
adding characters to the $index var at the top of the function body.
Bonus
Thanks to some wonderful contributions in the comment section, here are some interesting
updates & additions:
Pro Tip
You may want to remove vouwels (a, e, o, u, i) from $index as to avoid combinations that result
in: 'penis' or other dirty words that could get your customers upset.
You can also use the $pad_up argument to enforce a minimum length of 5 characters as to avoid:
'nsfw' and 'wtf'.
Thanks to William for pointing this out ; )
Postgres Implementation
Thanks to William as well:
END LOOP;
return output_text;
END;
$$ LANGUAGE plpgsql;
index :=
string_to_array('0,d,A,3,E,z,W,m,D,S,Q,l,K,s,P,b,N,c,f,j,5,I,t,C,i,y,o,G,2,r,x,h,V,J,k,-,T,w,H,L,9,e,
u,X,p,U,a,O,v,4,R,B,q,M,n,g,1,F,6,Y,_,8,7,Z', ',');
bits := string_to_bits(input_text);
return output_text;
END;
$$ LANGUAGE plpgsql;
Java Implementation
Thanks to Ant Kutschera there also is a Java version.
package uk.co.maxant.util;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* allows you to convert a whole number into a compacted representation of that number,
* based upon the dictionary you provide. very similar to base64 encoding, or indeed hex
* encoding.
*/
public class BaseX {
/**
* contains hexadecimals 0-F only.
*/
public static final char[] DICTIONARY_16 =
new char[]{'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
/**
* contains only alphanumerics, in capitals and excludes letters/numbers which can be
confused,
* eg. 0 and O or L and I and 1.
*/
public static final char[] DICTIONARY_32 =
new char[]
{'1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','J','K','M','N','P','Q','R','S','T','U','V','W','X'
,'Y','Z'};
/**
* contains only alphanumerics, including both capitals and smalls.
*/
public static final char[] DICTIONARY_62 =
new char[]
{'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','
U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
};
/**
* contains alphanumerics, including both capitals and smalls, and the following special
chars:
* +"@*#%&/|()=?'~[!]{}-_:.,; (you might not be able to read all those using a browser!
*/
public static final char[] DICTIONARY_89 =
new char[]
{'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','
U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
'+','"','@','*','#','%','&','/','|','(',')','=','?','~','[',']','{','}','$','-','_','.',':',',',';','<','>'};
/**
* create an encoder with the given dictionary.
*
* @param dictionary the dictionary to use when encoding and decoding.
*/
/**
* creates an encoder with the {@link #DICTIONARY_62} dictionary.
*
* @param dictionary the dictionary to use when encoding and decoding.
*/
public BaseX(){
this.dictionary = DICTIONARY_62;
}
/**
* tester method.
*/
public static void main(String[] args) {
String original = "123456789012345678901234567890";
System.out.println("Original: " + original);
BaseX bx = new BaseX(DICTIONARY_62);
/**
* encodes the given string into the base of the dictionary provided in the constructor.
* @param value the number to encode.
* @return the encoded string.
*/
public String encode(BigInteger value) {
int exponent = 1;
BigInteger remaining = value;
while(true){
BigInteger a = base.pow(exponent); //16^1 = 16
BigInteger b = remaining.mod(a); //119 % 16 = 7 | 112 % 256 = 112
BigInteger c = base.pow(exponent - 1);
BigInteger d = b.divide(c);
//finished?
if(remaining.equals(BigInteger.ZERO)){
break;
}
exponent++;
//need to reverse it, since the start of the list contains the least significant values
StringBuffer sb = new StringBuffer();
for(int i = result.size()-1; i >= 0; i--){
sb.append(result.get(i));
}
return sb.toString();
}
/**
* decodes the given string from the base of the dictionary provided in the constructor.
* @param str the string to decode.
* @return the decoded number.
*/
public BigInteger decode(String str) {
BigInteger bi = BigInteger.ZERO;
BigInteger base = new BigInteger("" + dictionary.length);
int exponent = 0;
for(char c : chars2){
BigInteger a = dictMap.get(c);
BigInteger b = base.pow(exponent).multiply(a);
return bi;
}
}
JavaScript Implementation
Thanks to Even Simon, there's a JavaScript implementation. You will also find PHP version
there, that implements the encode & decode functions as separate methods in a class.
/**
* 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 AlphabeticID = {
index:'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ',
/**
* @function AlphabeticID.encode
* @description Encode a number into short string
* @param integer
* @return string
**/
encode:function(_number){
if('undefined' == typeof _number){
return null;
}
else if('number' != typeof(_number)){
throw new Error('Wrong parameter type');
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 AlphabeticID.decode
* @description Decode a short string and return number
* @param string
* @return integer
**/
decode:function(_string){
if('undefined' == typeof _string){
return null;
}
else if('string' != typeof _string){
throw new Error('Wrong parameter type');
}
return ret;
},
/**
* @function AlphabeticID.bcpow
* @description Raise _a to the power _b
* @param float _a
* @param integer _b
* @return string
**/
bcpow:function(_a, _b){
return Math.floor(Math.pow(parseFloat(_a), parseInt(_b)));
}
};
/**
* @function String.reverse
* @description Reverse a string
* @return string
**/
String.prototype.reverse = function(){
return this.split('').reverse().join('');
};
C# Implementation
Thanks to Romas, there's a C# implementation.
Improved by [rumble
strip](https://twitter.com/rsadventure/status/481138491300933633)
class ShortId
{
public static readonly string Alphabet =
"abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
if (pad > 0)
{
result -= (ulong)BcPow(Alphabet.Length, pad);
}
return result;
}
if (pad > 0)
{
value += (ulong)BcPow(Alphabet.Length, pad);
}
result += Alphabet[(int)a];
value = value - (a * bcp);
}
return result.ReverseString();
}
Python Implementations
Thanks to wessite, there's a Python implementation.
ALPHABET = "bcdfghjklmnpqrstvwxyz0123456789BCDFGHJKLMNPQRSTVWXYZ"
BASE = len(ALPHABET)
MAXLEN = 6
pad = self.MAXLEN - 1
n = int(n + pow(self.BASE, pad))
s = []
t = int(math.log(n, self.BASE))
while True:
bcp = int(pow(self.BASE, t))
a = int(n / bcp) % self.BASE
s.append(self.ALPHABET[a:a+1])
n = n - (a * bcp)
t -= 1
if t < 0: break
return "".join(reversed(s))
n = "".join(reversed(n))
s=0
l = len(n) - 1
t=0
while True:
bcpow = int(pow(self.BASE, l - t))
s = s + self.ALPHABET.index(n[t:t+1]) * bcpow
t += 1
if t > l: break
pad = self.MAXLEN - 1
s = int(s - pow(self.BASE, pad))
return int(s)
Noah Miller contributed a version based on Wessite's, and changed it so it can use a passkey, and
rolled it into one function:
import math
import hashlib
ALPHABET = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
base = len(index)
if to_num:
idnum = idnum[::-1]
out = 0
length = len(idnum) -1
t=0
while True:
if pad_up:
pad_up -= 1
if pad_up > 0:
out -= int(pow(base, pad_up))
else:
if pad_up:
pad_up -= 1
if pad_up > 0:
idnum += int(pow(base, pad_up))
out = []
t = int(math.log(idnum, base))
while True:
bcp = int(pow(base, t))
a = int(idnum / bcp) % base
out.append(index[a:a+1])
idnum = idnum - (a * bcp)
t -= 1
if t < 0: break
out = ''.join(out[::-1])
return out
HaXe Implementation
Thanks to Andy Li, there's a HaXe implementation.
/**
* HaXe version of AlphabeticID
* Author: Andy Li <andy@onthewings.net>
* ported from...
*
* Javascript AlphabeticID class
* Author: Even Simon <even.simon@gmail.com>
* which is based on a script by Kevin van Zonneveld <kevin@vanzonneveld.net>)
*
class AlphaID {
static public var index:String =
'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
var i = 0;
var end = Math.floor(Math.log(_number)/Math.log(index.length));
while(i <= end) {
strBuf.add(index.charAt((Math.floor(_number / bcpow(index.length, i++)) %
index.length)));
}
return strBuf.toString();
}
var i = 0;
var end = str.length - 1;
while(i <= end) {
ret += Std.int(index.indexOf(str.charAt(i)) * (bcpow(index.length, end-i)));
++i;
}
return ret;
}
Go Implementation
Thanks to Dinesh Appavoo, there's a Go implementation.
// Package basex generates alpha id (alphanumeric id) for big integers. This
// is particularly useful for shortening URLs.
package basex
import (
"math/big"
"strconv"
)
var (
dictionary = []byte{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}
// Encode converts the big integer to alpha id (an alphanumeric id with mixed cases)
func Encode(val string) string {
var result []byte
var index int
var strVal string
base := big.NewInt(int64(len(dictionary)))
a := big.NewInt(0)
b := big.NewInt(0)
c := big.NewInt(0)
d := big.NewInt(0)
exponent := 1
remaining := big.NewInt(0)
remaining.SetString(val, 10)
for remaining.Cmp(big.NewInt(0)) != 0 {
//need to reverse it, since the start of the list contains the least significant values
return string(reverse(result))
}
j := 0
for _, val := range dictionary {
dictMap[val] = big.NewInt(int64(j))
j=j+1
}
bi := big.NewInt(0)
base := big.NewInt(int64(len(dictionary)))
exponent := 0
a := big.NewInt(0)
b := big.NewInt(0)
intermed := big.NewInt(0)
Posted in: php python java javascript sql programming mysql databaseyoutube tinyurl