├── .gitignore ├── README.md ├── ShortId.php └── composer.json /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | vendor 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PHP Short ID creator 2 | 3 | The library help you generate short id like youtube, vimeo, bit.ly, etc. Short generation (creation) based on numerical ID. 4 | 5 | ## Simple scenarios of using 6 | 7 | ```php 8 | require('vendor/autoload.php'); 9 | 10 | $shortId = new \kotchuprik\short_id\ShortId(); 11 | ``` 12 | 13 | ### Creating short ID for a record from in a database 14 | 15 | 1. when an app created a record in an your database with ID 424242 16 | 2. $shortId->encode(424242) encodes it to 'bLTs' 17 | 3. you updated the record for ID 424242 and set short_id of the record to 'bLTs' 18 | 19 | ```php 20 | $id = $shortId->encode(422424); // $id will be 'bLTs' 21 | 22 | // or with $neededLength = 6 23 | $id = $shortId->encode(422424, 6); // $id will be 'babMwC' 24 | ``` 25 | 26 | ### Searching record in a database 27 | 28 | 1. when someone requests rLHWfKd 29 | 2. $shortId->decode('rLHWfKd') decodes it to 424242 30 | 3. you found the record for ID 424242 in an your database 31 | 32 | ```php 33 | $id = $shortId->decode('bLTs'); // $id will be 424242 34 | 35 | // or with $neededLength = 6 36 | $id = $shortId->decode('babMwC', 6); // $id will be 424242 37 | ``` 38 | -------------------------------------------------------------------------------- /ShortId.php: -------------------------------------------------------------------------------- 1 | alphabet = $alphabet; 12 | } 13 | 14 | public function encode($input, $neededLength = 0) 15 | { 16 | $output = ''; 17 | $base = strlen($this->alphabet); 18 | if (is_numeric($neededLength)) { 19 | $neededLength--; 20 | if ($neededLength > 0) { 21 | $input += pow($base, $neededLength); 22 | } 23 | } 24 | for ($current = ($input != 0 ? floor(log($input, $base)) : 0); $current >= 0; $current--) { 25 | $powed = pow($base, $current); 26 | $floored = floor($input / $powed) % $base; 27 | $output = $output . substr($this->alphabet, $floored, 1); 28 | $input = $input - ($floored * $powed); 29 | } 30 | 31 | return $output; 32 | } 33 | 34 | public function decode($input, $neededLength = 0) 35 | { 36 | $output = 0; 37 | $base = strlen($this->alphabet); 38 | $length = strlen($input) - 1; 39 | for ($current = $length; $current >= 0; $current--) { 40 | $powed = pow($base, $length - $current); 41 | $output = ($output + strpos($this->alphabet, substr($input, $current, 1)) * $powed); 42 | } 43 | if (is_numeric($neededLength)) { 44 | $neededLength--; 45 | if ($neededLength > 0) { 46 | $output -= pow($base, $neededLength); 47 | } 48 | } 49 | 50 | return $output; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "kotchuprik/php-short-id", 3 | "description": "Yet another Short ID generator. The library help you generate short id like youtube, vimeo, bit.ly, etc.", 4 | "license": "MIT", 5 | "authors": [ 6 | { 7 | "name": "Constantine Chuprik", 8 | "email": "constantinchuprik@gmail.com" 9 | } 10 | ], 11 | "autoload": { 12 | "psr-4": { 13 | "kotchuprik\\short_id\\": "" 14 | } 15 | } 16 | } 17 | --------------------------------------------------------------------------------