visus/cuid2

A PHP library for generating collision-resistant ids (CUIDs).

Installs: 804 148

Dependents: 4

Suggesters: 0

Security: 0

Stars: 39

Watchers: 3

Forks: 1

Open Issues: 1

pkg:composer/visus/cuid2

5.2.0 2025-10-07 12:31 UTC

This package is auto-updated.

Last update: 2025-10-18 03:48:43 UTC


README

GitHub Workflow Status (with event)

Sonar Quality Gate Sonar Coverage Sonar Tests

PHP Version Packagist Downloads GitHub

A PHP implementation of collision-resistant ids. You can read more about CUIDs from the official project website.

Getting Started

You can install visus/cuid2 as a composer package:

composer require visus/cuid2

Tip

Consider installing/enabling the PHP extension GMP. If this is not an option then markrogoyski/math-php will be used as a fallback.

Examples

Instance Based

<?php
require_once 'vendor/autoload.php';

// new (default length of 24)
$cuid = new Visus\Cuid2\Cuid2();

// implicit casting
echo $cuid; // apr5hhh4ox45krsg9gycbs9k

// explicit casting
echo $cuid->toString(); // apr5hhh4ox45krsg9gycbs9k

// new (with custom length)
$cuid = new Visus\Cuid2\Cuid2(10);
echo $cuid; // pekw02xwsd

Static Based

<?php
require_once 'vendor/autoload.php';

// new (default length of 24)
$cuid = Visus\Cuid2\Cuid2::generate();

// implicit casting
echo $cuid; // apr5hhh4ox45krsg9gycbs9k

// explicit casting
echo $cuid->toString(); // apr5hhh4ox45krsg9gycbs9k

// new (with custom length)
$cuid = Visus\Cuid2\Cuid2::generate(10);
echo $cuid; // pekw02xwsd

Validation

Note

This method does not guarantee that the value is actually a CUID2, only that it follows the format.

<?php
require_once 'vendor/autoload.php';

Cuid2::isValid('apr5hhh4ox45krsg9gycbs9k'); // true
Cuid2::isValid('invalid-cuid'); // false
Cuid2::isValid('pekw02xwsd', expectedLength: 10); // true