gogh-idawoodjee/shortcode

Encode integers and UUIDs to short, URL-safe Base62 strings and back.

Maintainers

Package info

github.com/gogh-idawoodjee/shortcode

pkg:composer/gogh-idawoodjee/shortcode

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-05-26 21:56 UTC

This package is auto-updated.

Last update: 2026-05-26 22:04:22 UTC


README

Encode integers and UUIDs to short, URL-safe Base62 strings and back.

Turn 550e8400-e29b-41d4-a716-446655440000 into 5mSYFjfBLkNseKP0tQ8y0w and back again losslessly.

Encoding Characters UUID length
Hex (raw UUID) 0-9, a-f 32 chars
Base64 A-Z, a-z, 0-9, +, / 22 chars (needs padding/escaping)
Base62 A-Z, a-z, 0-9 ~22 chars (URL-safe, no escaping)

Base62 gives you near-optimal compression while staying alphanumeric — no +, /, =, or %-encoding needed.

Installation

composer require gogh-idawoodjee/shortcode

Requires PHP 8.2+ and the bcmath extension.

Usage

Integers

use GoghIdawoodjee\ShortCode\ShortCode;

ShortCode::encode(123456);    // "W7E"
ShortCode::decode('W7E');     // 123456

UUIDs

$uuid = '550e8400-e29b-41d4-a716-446655440000';

$short = ShortCode::encodeUuid($uuid);  // "5mSYFjfBLkNseKP0tQ8y0w"
$back  = ShortCode::decodeUuid($short); // "550e8400-e29b-41d4-a716-446655440000"

UUIDs with or without dashes are both accepted. The output is always alphanumeric so it's safe for URLs, filenames, and QR codes.

Laravel Facade

A Laravel facade is included and auto-discovered — no manual registration needed.

use GoghIdawoodjee\ShortCode\Facades\ShortCode;

ShortCode::encodeUuid($uuid);
ShortCode::decodeUuid($code);

Error Handling

All methods throw InvalidArgumentException on bad input:

ShortCode::encode(-1);           // negative number
ShortCode::decode('abc!');       // invalid character
ShortCode::encodeUuid('nope');   // not a valid UUID

Testing

composer test

License

MIT