PHP code example of visus / cuid2

1. Go to this page and download the library: Download visus/cuid2 library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

visus / cuid2 example snippets



Visus\Cuid2\Cuid2;

// Generate an identifier with the default length of 24 characters
$cuid = new Cuid2();

// Cast the identifier to a string implicitly
echo $cuid; // p6p168tx2rxtgyehd3p2wz04

// Cast the identifier to a string explicitly
echo $cuid->toString(); // p6p168tx2rxtgyehd3p2wz04

// Generate an identifier with a custom length, from 4 to 32 characters
$shortCuid = new Cuid2(10);
echo $shortCuid; // a1ao2r0lve


Visus\Cuid2\Cuid2;

// Generate an identifier with the default length of 24 characters
$cuid = Cuid2::generate();

// Cast the identifier to a string implicitly
echo $cuid; // zbc8kp9qqoh3pvseey6m7nrq

// Cast the identifier to a string explicitly
echo $cuid->toString(); // zbc8kp9qqoh3pvseey6m7nrq

// Generate an identifier with a custom length, from 4 to 32 characters
$shortCuid = Cuid2::generate(10);
echo $shortCuid; // rywe9nkxrx


Visus\Cuid2\Cuid2;

// Validate the format
Cuid2::isValid('p6p168tx2rxtgyehd3p2wz04'); // true
Cuid2::isValid('invalid-cuid'); // false

// Validate the format and check the expected length
Cuid2::isValid('a1ao2r0lve', expectedLength: 10); // true
Cuid2::isValid('a1ao2r0lve', expectedLength: 24); // false