PHPackages                             pugx/shortid-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. pugx/shortid-php

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

pugx/shortid-php
================

An implementation of shortid in PHP

v1.4.0(1mo ago)51627.6k↓44.1%73MITPHPPHP ^8.2CI passing

Since Feb 27Pushed 3w ago7 watchersCompare

[ Source](https://github.com/PUGX/shortid-php)[ Packagist](https://packagist.org/packages/pugx/shortid-php)[ Docs](http://pugx.org/)[ RSS](/packages/pugx-shortid-php/feed)WikiDiscussions master Synced 2w ago

READMEChangelog (5)Dependencies (8)Versions (16)Used By (3)

ShortId
=======

[](#shortid)

[![Latest Stable Version](https://camo.githubusercontent.com/6dcba850b7b527ec27a71ba7995072c1634c05ed8934df4cd319325bec021287/68747470733a2f2f706f7365722e707567782e6f72672f707567782f73686f727469642d7068702f76)](https://packagist.org/packages/pugx/shortid-php)[![Total Downloads](https://camo.githubusercontent.com/090d3e061196c65373f0be80b116caf051a823218d305c03821a5ac362534c88/68747470733a2f2f706f7365722e707567782e6f72672f707567782f73686f727469642d7068702f646f776e6c6f616473)](https://packagist.org/packages/pugx/shortid-php)[![PHP Version Require](https://camo.githubusercontent.com/821d9bbc03f333a7b37c14b68eb1088f4368dc3377e4f7c4598c30d598b59bc7/68747470733a2f2f706f7365722e707567782e6f72672f707567782f73686f727469642d7068702f726571756972652f706870)](https://packagist.org/packages/pugx/shortid-php)[![Build Status](https://github.com/PUGX/shortid-php/workflows/build/badge.svg)](https://github.com/PUGX/shortid-php/workflows/build/badge.svg)[![Code Climate](https://camo.githubusercontent.com/1a065dae091a52d9dd57c5e5a79501a0b43ce5a52eda9ff36637e5c176db7dbf/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f505547582f73686f727469642d7068702f6261646765732f6770612e737667)](https://codeclimate.com/github/PUGX/shortid-php)[![Test Coverage](https://camo.githubusercontent.com/527e1aa71238ec09e8b4be55d228effa474c5a06878208ac53b467bb3088ab6e/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f505547582f73686f727469642d7068702f6261646765732f636f7665726167652e737667)](https://codeclimate.com/github/PUGX/shortid-php/coverage)[![codecov](https://camo.githubusercontent.com/9dad06e041d304c0776cde0c92060397c74b65e05a277827cc9b2f22d1007ec3/68747470733a2f2f636f6465636f762e696f2f67682f505547582f73686f727469642d7068702f6272616e63682f6d61737465722f67726170682f62616467652e7376673f746f6b656e3d33735836536858675a70)](https://codecov.io/gh/PUGX/shortid-php)[![License](https://camo.githubusercontent.com/63dc4e03e111d8e1e016db586c5fffaadcb583fd2d795f891145e1c239577d83/68747470733a2f2f706f7365722e707567782e6f72672f707567782f73686f727469642d7068702f6c6963656e73652e737667)](https://packagist.org/packages/pugx/shortid-php)

This library is an implementation of [ShortId](https://github.com/dylang/shortid) for PHP.

Installation
------------

[](#installation)

Install the library via Composer:

```
composer require pugx/shortid-php
```

Usage
-----

[](#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.

Basic usage
-----------

[](#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
--------------

[](#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.

```
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:

```
use PUGX\Shortid\Shortid;

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

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

More readable strings
---------------------

[](#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 the `generate` method. Notice that in this case the alphabet will be ignored, so it makes sense to pass a null one.

Example:

```
use PUGX\Shortid\Shortid;

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

$id = Shortid::generate(7, null, true);
```

Pre-defined values
------------------

[](#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.

```
use PUGX\Shortid\Shortid;

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

$myFixedId = new Shortid('5h0r71d');
$anotherFixedId = new Shortid('fooBarZ');
```

Doctrine
--------

[](#doctrine)

If you want to use ShortId with Doctrine ORM, take a look at [ShortId Doctrine type](https://github.com/PUGX/shortid-doctrine).

Doctrine and Symfony
--------------------

[](#doctrine-and-symfony)

If you want to use ShortId with Doctrine ORM and Symfony framework, take a look to [ShortId Doctrine type bundle](https://github.com/PUGX/shortid-doctrine-bundle).

Dependencies replacement
------------------------

[](#dependencies-replacement)

This library uses [a polyfill](https://github.com/symfony/polyfill-mbstring), so it can be used in environments where the mbstring native extension is not available.

If, instead, your environment is offering that extension, you can avoid installing that polyfill by configuring a [replace](https://getcomposer.org/doc/04-schema.md#replace) entry in your `composer.json`.

Contributing
------------

[](#contributing)

Contributions are welcome. Feel free to open a Pull Request or file an issue here on GitHub! Please read the [contributing guidelines](CONTRIBUTING.md) first.

###  Health Score

67

—

FairBetter than 99% of packages

Maintenance94

Actively maintained with recent releases

Popularity49

Moderate usage in the ecosystem

Community24

Small or concentrated contributor base

Maturity83

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 92.4% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~270 days

Recently: every ~419 days

Total

15

Last Release

36d ago

Major Versions

v0.7.0 → v1.0.02021-12-08

PHP version history (7 changes)v0.1.0PHP &gt;=5.4

v0.2.0PHP &gt;=7.0

v0.4.0PHP ^7.0

v0.5.1PHP ^7.1

v0.7.0PHP ^7.1 || ^8.0

v1.3.0PHP ^8.1

v1.4.0PHP ^8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/d191f9ff3aa52357cb523c9eec3553d267128fc75e0faf9dca74f289f6604f54?d=identicon)[garak](/maintainers/garak)

---

Top Contributors

[![garak](https://avatars.githubusercontent.com/u/179866?v=4)](https://github.com/garak "garak (85 commits)")[![acelaya](https://avatars.githubusercontent.com/u/2719332?v=4)](https://github.com/acelaya "acelaya (2 commits)")[![EmanueleMinotto](https://avatars.githubusercontent.com/u/417201?v=4)](https://github.com/EmanueleMinotto "EmanueleMinotto (2 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (1 commits)")[![EgorGruzdev](https://avatars.githubusercontent.com/u/930669?v=4)](https://github.com/EgorGruzdev "EgorGruzdev (1 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (1 commits)")

---

Tags

hacktoberfestphpshortiduuididshortid

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/pugx-shortid-php/health.svg)

```
[![Health](https://phpackages.com/badges/pugx-shortid-php/health.svg)](https://phpackages.com/packages/pugx-shortid-php)
```

###  Alternatives

[friendsofphp/php-cs-fixer

A tool to automatically fix PHP code style

13.5k257.0M28.0k](/packages/friendsofphp-php-cs-fixer)[symfony/filesystem

Provides basic utilities for the filesystem

4.6k725.1M4.3k](/packages/symfony-filesystem)[symfony/dom-crawler

Eases DOM navigation for HTML and XML documents

4.0k407.4M2.8k](/packages/symfony-dom-crawler)[matomo/matomo

Matomo is the leading Free/Libre open analytics platform

21.7k39.6k](/packages/matomo-matomo)[helsingborg-stad/municipio

A bootstrap theme for creating municipality sites.

4028.6k10](/packages/helsingborg-stad-municipio)[japanese-date/japanese-date

日本の暦、祝日を取り扱うライブラリ

1610.1k](/packages/japanese-date-japanese-date)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
