PHPackages                             ex3v/random-string-key-generator - 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. ex3v/random-string-key-generator

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

ex3v/random-string-key-generator
================================

Library to generate random and unique YouTube-like string ids, as an alternative to UUID

213.5k↓65.6%1[1 PRs](https://github.com/Ex3v/random-string-key-generator/pulls)PHPCI failing

Since Jun 27Pushed 6y agoCompare

[ Source](https://github.com/Ex3v/random-string-key-generator)[ Packagist](https://packagist.org/packages/ex3v/random-string-key-generator)[ RSS](/packages/ex3v-random-string-key-generator/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependenciesVersions (1)Used By (0)

### Random String Key Generator

[](#random-string-key-generator)

This little library makes it easy to generate short, unique, YouTube-like IDs that you can use instead of UUID in your application.

Using shorter (but still unique) ids will have positive inpact on database performance (especially MySQL, which don't handle UUID well).

This library consists of both generator itself, as well as Doctrine integration.

It allows you to customize length of default generator, as well as to write generators of your own.

##### Will it REALLY generate short unique ids?

[](#will-it-really-generate-short-unique-ids)

Let's look at how many unique values can standard 8 characters long sequence have:

```
Example id:

9ZGpFadq

8 characters, each can have one of 64 values.

64^8 = 281 474 976 710 656 different values

```

In the end even UUID might not be unique, it depends on the way you see it. In the end, unless you're using really short ids, it's very close to impossible to get two duplicate strings. I ran multiple tests of generating large amounts of ids (tens of millions) in very short time and couldn't make it to get two duplicate ids.

If you are really afraid of duplicate values, you can extend the key length, but in general this is not neccesary.

### 1. Installation

[](#1-installation)

Use Composer to install it:

```
composer require ex3v/random-string-key-generator
```

### 2. Usage

[](#2-usage)

#### Generating ID manually

[](#generating-id-manually)

```
$factory   = new GeneratorFactory();
$generator = $factory->getGenerator(); //will give you default generator with basic config

$id = $generator->generateId();
```

#### Generating ID automatically in Doctrine

[](#generating-id-automatically-in-doctrine)

Just set up generator annotations (`GeneratedValue(strategy="CUSTOM")` and `CustomIdGenerator(...)`) on top of field declaration.

```
/**
 * @var string
 *
 * @ORM\Id()
 * @ORM\Column(name="id", type="string")
 * @ORM\GeneratedValue(strategy="CUSTOM")
 * @ORM\CustomIdGenerator(class="Ex3v\RandomStringKeyGenerator\Doctrine\RandomStringKeyGenerator")
 */
 private $id;
```

#### Changing default settings

[](#changing-default-settings)

This library comes with single generator, that returns 8 characters long strings.

##### Changing default key length and other default settings

[](#changing-default-key-length-and-other-default-settings)

```
//now default generator will return 12 characters long keys
GeneratorFactory::setDefaultStrategy(UniqIdBasedGenerator::class, [GeneratorInterface::KEY_LENGTH => 12]);
```

##### Getting instance of your own generator

[](#getting-instance-of-your-own-generator)

```
$factory = new GeneratorFactory();

$generator = $factory->getGenerator(YourGeneratorImplementingGeneratorInterface::class, ['generator' => 'options']);
```

##### I want to change default generator config for Doctrine and Symfony. How can I do that?

[](#i-want-to-change-default-generator-config-for-doctrine-and-symfony-how-can-i-do-that)

Unfortunately, Doctrine metadata factory restricts from passing any parameters or container parameters/services to ID Generators. Your only option here is to set GeneratorFactory defaults before app initialization. In case of Symfony, the place you're looking for is Kernel class.

### 3. Testing

[](#3-testing)

Just run

```
composer test
```

in this repository, to run the test suite.

### 4. Contribution

[](#4-contribution)

Have idea on how to develop it? Found a bug and want to fix it? Pull Requests and Issues are welcome :)

###  Health Score

25

—

LowBetter than 35% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity27

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity37

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% 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.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/2785665?v=4)[Maciej Szkamruk](/maintainers/Ex3v)[@Ex3v](https://github.com/Ex3v)

---

Top Contributors

[![Ex3v](https://avatars.githubusercontent.com/u/2785665?v=4)](https://github.com/Ex3v "Ex3v (5 commits)")

### Embed Badge

![Health badge](/badges/ex3v-random-string-key-generator/health.svg)

```
[![Health](https://phpackages.com/badges/ex3v-random-string-key-generator/health.svg)](https://phpackages.com/packages/ex3v-random-string-key-generator)
```

###  Alternatives

[denngarr/seat-fitting

Module to check fittings per character

1433.3k2](/packages/denngarr-seat-fitting)[thedmsgroup/mautic-enhancer-bundle

Various contact enhancer integrations for Mautic.

342.8k](/packages/thedmsgroup-mautic-enhancer-bundle)

PHPackages © 2026

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