ShortId
, (*1)
This library is an implementation of ShortId for PHP., (*2)
Installation
Install the library via Composer:, (*3)
composer require pugx/shortid-php
Usage
ShortId is a PHP library that generates short, unique, and random strings. It's useful in scenarios
where you need concise identifiers, such as URL shortening or generating unique keys., (*4)
Basic usage
Just call PUGX\Shortid\Shortid::generate()
to get a random string with default length 7, like "MfiYIvI"., (*5)
``` php
use PUGX\Shortid\Shortid;, (*6)
require_once DIR.'/vendor/autoload.php';, (*7)
$id = Shortid::generate();, (*8)
## Advanced usage
For more control, you can customize the alphabet and length using the Factory class.
The default alphabet uses all letters (lowercase and uppercase), all numbers, underscore, and hyphen.
``` php
use PUGX\Shortid\Factory;
use PUGX\Shortid\Shortid;
require_once __DIR__.'/vendor/autoload.php';
$factory = new Factory();
// alphabet string must be 64 characters long
$factory->setAlphabet('Ă©123456789Ă bcdefghĂŹjklmnĂČpqrstĂčvwxyzABCDEFGHIJKLMNOPQRSTUVWX.!@|');
// length must be between 2 and 20 (default is 7)
// of course, a lower length increases the clashing probability
$factory->setLength(9);
Shortid::setFactory($factory);
$id = Shortid::generate();
As an alternative, you can customize single generations:, (*9)
``` php
use PUGX\Shortid\Shortid;, (*10)
require_once DIR.'/vendor/autoload.php';, (*11)
$id9 = Shortid::generate(9, 'Ă©123456789Ă bcdefghĂŹjklmnĂČpqrstĂčvwxyzABCDEFGHIJKLMNOPQRSTUVWX.!@|');
$id5 = Shortid::generate(5);, (*12)
## More readable strings
Sometimes, you want to avoid some ambiguous characters, like `B`/`8` or `I`/`l` (uppercase/lowercase).
In this case, you can pass a third parameter `true` to `generate` method. Notice that in this case the alphabet
will be ignored, so it makes sense to pass a null one.
Example:
``` php
use PUGX\Shortid\Shortid;
require_once __DIR__.'/vendor/autoload.php';
$id = Shortid::generate(7, null, true);
Pre-defined values
If you need a deterministic string, instead of a random one, you can call directly the class constructor.
This could be useful, for instance, when you need pre-defined data for testing purposes., (*13)
``` php
use PUGX\Shortid\Shortid;, (*14)
require_once DIR.'/vendor/autoload.php';, (*15)
$myFixedId = new Shortid('5h0r71d');
$anotherFixedId = new Shortid('fooBarZ');, (*16)
```, (*17)
Doctrine
If you want to use ShortId with Doctrine ORM, take a look at ShortId Doctrine type., (*18)
Doctrine and Symfony
If you want to use ShortId with Doctrine ORM and Symfony framework, take a look to
ShortId Doctrine type bundle., (*19)
Dependencies replacement
This library uses a polyfill, so it can be used in environments where the mbstring
native extension is not available., (*20)
If, instead, your environment is offering that extension, you can avoid installing
that polyfill by configuring a replace entry in your composer.json
., (*21)
The same applies to the randomLib library: if you are using PHP 8.3 or higher,
you can replace it, since this library uses the native Random
class instead., (*22)
Contributing
Contributions are welcome. Feel free to open a Pull Request or file an issue here on GitHub!
Please read the [contributing guidelines][7] first., (*23)