Nanoid-PHP

A tiny, secure, URL-friendly unique string ID generator for PHP, ported from JavaScript's nanoid.

Library
Composer
v2.0
815stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
38/100Needs Attention
Development Activity0
Maintenance20
Community44
Maturity60
Momentum28

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
65/100Good
Architecture60
Code Quality62
Innovation58
Learning Curve80

Nanoid-PHP is a faithful PHP port of the popular JavaScript nanoid library, giving PHP applications the same tiny footprint and cryptographically strong randomness for generating unique, URL-safe identifiers. It uses more symbols than a UUID (A-Za-z0-9_-) while achieving the same collision probability in just 21 characters, making it a lightweight drop-in replacement anywhere a UUID would normally be used.

The library exposes a simple Client class with a default secure random generator plus a dynamic mode, and supports fully custom alphabets, custom ID lengths, and pluggable random-byte generators via a GeneratorInterface. Its minimal surface area, permissive MIT license, and zero runtime dependencies beyond paragonie/random_compat make it easy to audit and drop into any PHP codebase that needs short, safe, unique identifiers.

What You Get

  • A Client class for generating default 21-character URL-friendly unique IDs with a single method call
  • A safer ‘dynamic’ random mode alongside the default secure random generator
  • Support for fully custom alphabets and custom ID lengths via formattedId()
  • A GeneratorInterface for plugging in your own random-byte source
  • A small, dependency-light codebase (under 200 lines of core PHP) that’s easy to audit

Common Use Cases

  • Generating short, URL-safe identifiers for public-facing resources instead of long UUIDs
  • Creating unique keys for cache entries, short links, or session tokens
  • Producing collision-resistant IDs for database records without a central sequence
  • Replacing UUID v4 in contexts where a shorter, still-secure identifier is preferred

Under The Hood

Architecture - The library is organized around a small set of focused classes under src/: Client is the primary entry point exposing generateId() and formattedId(), Core implements the shared ID-building logic against a CoreInterface contract, and Generator implements GeneratorInterface to produce arrays of random bytes. This separation lets callers swap in a custom random source (e.g., for testing or specialized entropy needs) while reusing the same alphabet-formatting logic.

Tech Stack - The project targets PHP 7.1 through 8.x and depends only on paragonie/random_compat for a polyfilled, cryptographically secure random-bytes function on older PHP versions. It ships as a Composer package with PSR-4 autoloading (Hidehalo\Nanoid\) and no other runtime dependencies.

Code Quality - The tests/ directory contains PHPUnit test classes (ClientTest, CoreTest, GeneratorTest) covering the core generation paths, and the project enforces PSR-2 style via squizlabs/php_codesniffer with composer check-style/fix-style scripts. The codebase itself is compact (roughly 200 lines across five files), which keeps the surface area easy to review line by line.

API Design - The public API is deliberately minimal: instantiate a Client and call generateId($size) for the common case, or formattedId($alphabet, $size, $generator) for full control over alphabet and randomness source. This mirrors the ergonomics of the original JavaScript nanoid, so developers familiar with that library can adopt the PHP port with almost no learning curve.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search