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

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

hidehalo/nanoid-php
===================

A copy of nanoid in PHP

2.0(1y ago)8124.1M—3.9%57[1 PRs](https://github.com/hidehalo/nanoid-php/pulls)20MITPHPPHP ~7.1|~8.0

Since Sep 11Pushed 1y ago7 watchersCompare

[ Source](https://github.com/hidehalo/nanoid-php)[ Packagist](https://packagist.org/packages/hidehalo/nanoid-php)[ Docs](https://github.com/hidehalo/nanoid-php)[ RSS](/packages/hidehalo-nanoid-php/feed)WikiDiscussions 2.x Synced 1mo ago

READMEChangelog (10)Dependencies (3)Versions (22)Used By (20)

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

[](#nanoid-php)

[![Build Status](https://camo.githubusercontent.com/6e73fae483f97e9e2361aabf2f114abd3519767fb08f71244f5ebc854e780d89/68747470733a2f2f7472617669732d63692e6f72672f6869646568616c6f2f656d6f6a692e737667)](https://travis-ci.org/hidehalo/nanoid-php)[![FOSSA Status](https://camo.githubusercontent.com/996a59abbc49f2005ff53fcc988b6c9794f4a059a45020357aa2899332032ddb/68747470733a2f2f6170702e666f7373612e636f6d2f6170692f70726f6a656374732f6769742532426769746875622e636f6d2532466869646568616c6f2532466e616e6f69642d7068702e7376673f747970653d736869656c64)](https://app.fossa.com/projects/git%2Bgithub.com%2Fhidehalo%2Fnanoid-php?ref=badge_shield)

> 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.

Thanks awesome [ai](https://github.com/ai) and his [nanoid](https://github.com/ai/nanoid), this package is a copy in PHP! If you like nanoid and you want to use it in PHP, try me :D

Install
-------

[](#install)

Via Composer

```
composer require hidehalo/nanoid-php
```

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 Hidehalo\Nanoid\Client;
use Hidehalo\Nanoid\GeneratorInterface;

$client = new Client();

# default random generator
echo $client->generateId($size = 21);
# more safer random generator
echo $client->generateId($size = 21, $mode = Client::MODE_DYNAMIC);
```

### Custom Alphabet or Length

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

```
echo $client->formattedId($alphabet = '0123456789abcdefg', $size = 21);
```

> 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
echo $client->formattedId($alphabet = '0123456789abcdefg', $size = 21,
new class implements GeneratorInterface {
    /**
     * @inheritDoc
     */
    public function random($size)
    {
        //TODO: implemenation ...
    }
});
```

> `random` callback must accept the array size and return an array with random numbers.
>
> If you want to use the same URL-friendly symbols with `format`, you can get default alphabet from the `url` module:

Please see [`CoreInterface::random(...)`](src/CoreInterface.php) for the core random API prototype and notes

Examples
--------

[](#examples)

Please see [Examples](examples) for more information on detailed usage.

Change log
----------

[](#change-log)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Testing
-------

[](#testing)

```
composer test
```

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

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) and [CODE\_OF\_CONDUCT](CODE_OF_CONDUCT.md) for details.

Notice
------

[](#notice)

If you have any issues, just feel free and open it in this repository, thx!

Credits
-------

[](#credits)

- [ai](https://github.com/ai)
- [hidehalo](https://github.com/hidehalo)
- [All Contributors](https://github.com/hidehalo/nanoid-php/graphs/contributors)

License
-------

[](#license)

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

[![FOSSA Status](https://camo.githubusercontent.com/62c2aafef7bb0b744faf7f4963f3e9c7e491889934fc8b6b705acad5f202e60f/68747470733a2f2f6170702e666f7373612e636f6d2f6170692f70726f6a656374732f6769742532426769746875622e636f6d2532466869646568616c6f2532466e616e6f69642d7068702e7376673f747970653d6c61726765)](https://app.fossa.com/projects/git%2Bgithub.com%2Fhidehalo%2Fnanoid-php?ref=badge_large)

###  Health Score

58

—

FairBetter than 98% of packages

Maintenance38

Infrequent updates — may be unmaintained

Popularity66

Solid adoption and visibility

Community38

Small or concentrated contributor base

Maturity77

Established project with proven stability

 Bus Factor1

Top contributor holds 75% 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 ~124 days

Recently: every ~260 days

Total

22

Last Release

561d ago

Major Versions

1.x-dev → 2.02024-11-04

PHP version history (3 changes)1.0.0-alphaPHP ~5.6|~7.0

1.1.8PHP ~5.6|~7.0|~8.0

2.0PHP ~7.1|~8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/6302952?v=4)[\_\_hidehalo](/maintainers/hidehalo)[@hidehalo](https://github.com/hidehalo)

---

Top Contributors

[![hidehalo](https://avatars.githubusercontent.com/u/6302952?v=4)](https://github.com/hidehalo "hidehalo (45 commits)")[![andrewminion-luminfire](https://avatars.githubusercontent.com/u/47227887?v=4)](https://github.com/andrewminion-luminfire "andrewminion-luminfire (3 commits)")[![gabrielsosa-stensul](https://avatars.githubusercontent.com/u/60671278?v=4)](https://github.com/gabrielsosa-stensul "gabrielsosa-stensul (2 commits)")[![betaSavvy](https://avatars.githubusercontent.com/u/9004457?v=4)](https://github.com/betaSavvy "betaSavvy (2 commits)")[![kocoten1992](https://avatars.githubusercontent.com/u/7130705?v=4)](https://github.com/kocoten1992 "kocoten1992 (1 commits)")[![matt-harvey](https://avatars.githubusercontent.com/u/6045346?v=4)](https://github.com/matt-harvey "matt-harvey (1 commits)")[![pcambra](https://avatars.githubusercontent.com/u/104912?v=4)](https://github.com/pcambra "pcambra (1 commits)")[![acelaya](https://avatars.githubusercontent.com/u/2719332?v=4)](https://github.com/acelaya "acelaya (1 commits)")[![sjokkateer](https://avatars.githubusercontent.com/u/27890746?v=4)](https://github.com/sjokkateer "sjokkateer (1 commits)")[![fossabot](https://avatars.githubusercontent.com/u/29791463?v=4)](https://github.com/fossabot "fossabot (1 commits)")[![iGusev](https://avatars.githubusercontent.com/u/1555767?v=4)](https://github.com/iGusev "iGusev (1 commits)")[![jdecool](https://avatars.githubusercontent.com/u/433926?v=4)](https://github.com/jdecool "jdecool (1 commits)")

---

Tags

lightweightnanoidphpunique-idurl-friendlyuuid-likehidehalonanoid-php

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

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

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

###  Alternatives

[paragonie/random-lib

A Library For Generating Secure Random Numbers

703.3M26](/packages/paragonie-random-lib)[jasonhinkle/php-gpg

GPG / PGP port written in pure PHP with no binary dependencies

116147.2k1](/packages/jasonhinkle-php-gpg)[gpslab/base64uid

Generate UID like YouTube

68103.4k](/packages/gpslab-base64uid)[wyrihaximus/react-child-process-messenger

Messenger decorator for react/child-process

32279.4k4](/packages/wyrihaximus-react-child-process-messenger)[andrewcarteruk/cryptokey

A command line tool for generating keys using a CSPRNG.

6124.2k](/packages/andrewcarteruk-cryptokey)

PHPackages © 2026

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