PHPackages                             better/nanoid - 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. better/nanoid

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

better/nanoid
=============

A copy of nanoid in PHP

0.0.1(5y ago)72.7k↓50%1MITPHPPHP &gt;=7.1

Since Sep 21Pushed 5y agoCompare

[ Source](https://github.com/Microtribute/better-nanoid)[ Packagist](https://packagist.org/packages/better/nanoid)[ Docs](https://github.com/Microtribute/better-nanoid)[ RSS](/packages/better-nanoid/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)DependenciesVersions (2)Used By (1)

Nanoid-php
==========

[](#nanoid-php)

Overview
--------

[](#overview)

Replacement of [hidehalo/nanoid-php](https://github.com/hidehalo/nanoid-php) to work with PHP 7.1+

> A tiny (179 bytes), secure URL-friendly unique string ID generator for JavaScript
>
> **Safe.** It uses cryptographically strong random APIs and guarantees a proper distribution of symbols.
>
> **Small.** Only 179 bytes (minified and gzipped). No dependencies. It uses Size Limit to control size.
>
> **Compact.** It uses more symbols than UUID (A-Za-z0-9\_-) and has the same number of unique options in just 21 symbols instead of 36.

Install
-------

[](#install)

Via Composer

```
composer require better/nanoid
```

Usage
-----

[](#usage)

### Normal

[](#normal)

> The main module uses URL-friendly symbols (A-Za-z0-9\_-) and returns an ID with 21 characters (to have the same collisions probability as UUID v4).

```
use Better\Nanoid\Client;
use Better\Nanoid\GeneratorInterface;

$client = new Client();

# default random generator
echo $client->produce($size = 32);

# more secure generator with more entropy
echo $client->produce($size = 32, true);
```

### Custom Alphabet or Length

[](#custom-alphabet-or-length)

```
$client = new Client('0123456789abcdefg');
$client->produce();
```

> Alphabet must contain 256 symbols or less. Otherwise, the generator will not be secure.

### Custom Random Bytes Generator

[](#custom-random-bytes-generator)

```
# PS: anonymous class is new feature when PHP_VERSION >= 7.0

$client = new Client();

echo $client->produceUsing(new class implements GeneratorInterface {
    /**
     * @inheritDoc
     */
    public function random(int $size): string
    {
        $ret = [];

        while ($size--) {
            $ret[] = mt_rand(0, 255);
        }

        return $ret;
    }
});
```

> `random` callback must accept the array size and return an array with random numbers.

Credits
-------

[](#credits)

- [ai](https://github.com/ai)
- [hidehalo](https://github.com/hidehalo)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity24

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity39

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.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

2065d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/8507cb3da01f9ea75bfb77de3ecb488484b8c5032d2433e4945f4327bb605bde?d=identicon)[Microtribute](/maintainers/Microtribute)

---

Top Contributors

[![Microtribute](https://avatars.githubusercontent.com/u/46481195?v=4)](https://github.com/Microtribute "Microtribute (1 commits)")

---

Tags

nanorandom-generationunique-idphpbetternanoid

### Embed Badge

![Health badge](/badges/better-nanoid/health.svg)

```
[![Health](https://phpackages.com/badges/better-nanoid/health.svg)](https://phpackages.com/packages/better-nanoid)
```

###  Alternatives

[imanghafoori/laravel-anypass

A minimal yet powerful package to help you in development.

21421.6k](/packages/imanghafoori-laravel-anypass)

PHPackages © 2026

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