An implementation of shortid in PHP

v0.2.1 2017-03-19 18:29 UTC


Total Downloads Build Status Code Climate Test Coverage SensioLabsInsight

This library is an implementation of ShortId for PHP.

Basic usage

Just call PUGX\Shortid\Shortid::generate() to get a random string with default length 7, like "MfiYIvI".

use PUGX\Shortid\Shortid;

require_once __DIR__.'/vendor/autoload.php';

$id = Shortid::generate();

Advanced usage

In the following example, you can see how to change the basic alphabet and default length.

Default alphabet uses all letters (lowercase and uppercase), all numbers, underscore, and hypen.

use PUGX\Shortid\Factory;
use PUGX\Shortid\Shortid;

require_once __DIR__.'/vendor/autoload.php';

$factory = new Factory();
// alphabet string must be 64 characters long
// length must be between 2 and 20 (default is 7)
// of course, a lower length is increasing clashing probability

$id = Shortid::generate();

As alternative, you can customize single generations:

use PUGX\Shortid\Shortid;

require_once __DIR__.'/vendor/autoload.php';

$id9 = Shortid::generate(9, 'é123456789àbcdefghìjklmnòpqrstùvwxyzABCDEFGHIJKLMNOPQRSTUVWX.!@|');
$id5 = Shortid::generate(5);


If you want to use ShortId with Doctrine ORM, take a look to ShortId Doctrine type.

Doctrine and Symfony

If you want to use ShortId with Doctrine ORM and Symfony framework, take a look to ShortId Doctrine type bundle.