Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 99e13d5320
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 68 lines (59 sloc) 1.614 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
<?php

/**
* Tiny
*
* A reversible base62 ID obfuscater
*
* Authors:
* Jacob DeHart (original PHP version) and Kyle Bragger (Ruby port)
*
* Usage:
* $obfuscated_id = toTiny(123)
* $original_id = reverseTiny($obfuscated_id)
*
* Configuration:
* * You must run Tiny::generate_set() from console (using tiny.rb) to generate your $set
* Do *not* change this once you start using Tiny, as you won't be able to reverseTiny()
* any values toTiny()'ed with another set.
*
*/

class Tiny {
    public static $set = '__PUT SET HERE__';

    public static function toTiny($id){
        $set = self::$set;

        $HexN="";
        $id = floor(abs(intval($id)));
        $radix = strlen($set);
        while (true) {
            $R=$id%$radix;
            $HexN = $set{$R}.$HexN;
            $id=($id-$R)/$radix;
            if ($id==0) break;
        }
        return $HexN;
    }

    public static function reverseTiny($str){
        $set = self::$set;
        $radix = strlen($set);
        $strlen = strlen($str);
        $N = 0;
        for($i=0;$i<$strlen;$i++){
            $N += strpos($set,$str{$i})*pow($radix,($strlen-$i-1));
        }
        return "{$N}";
    }

    public static function generate_set(){
        $arr = array();
        for ($i = 65; $i <= 122; $i++)
        {
            if ($i < 91 || $i > 96) $arr[] = chr($i);
        }
        $arr = array_merge($arr, range(0, 9));
        shuffle($arr);
        return join('', $arr);
    }

}

// Testing
echo Tiny::generate_set() . "<br />";
echo Tiny::toTiny(123) . "<br />";
echo Tiny::reverseTiny(Tiny::toTiny(123));
Something went wrong with that request. Please try again.