Search by

byjg / shortid

byjg

Create short string IDs from numbers

Package info

github.com/byjg/php-shortid

pkg:composer/byjg/shortid

Statistics

Installs: 11 000

Dependents: 0

Suggesters: 0

Stars: 3

Open Issues: 0

6.0.0 2025-11-22 01:56 UTC

This package is auto-updated.

Last update: 2026-10-25 23:49:55 UTC


README

sidebar_key shortid
sidebar_category text-content
tags
php

ShortID

Turn numbers and UUIDs into short, URL-friendly strings, and back.

Sponsor Build Status Opensource ByJG Install MCP Server GitHub source GitHub license GitHub release

Why

Sequential ids in URLs (/order/1042) are long, reveal how many records you have, and invite people to guess the next one. A UUID solves that but is 36 characters long.

ShortId encodes the number (or the UUID) into a short string and decodes it back, with no database lookup:

$id = ShortId::fromNumber(1234567890);   // short string
ShortId::get($id);                       // 1234567890
ShortId::fromUuid('092395A6-BC87-11ED-8CA9-0242AC120002');

Use your own alphabet (custom maps) so the ids of your application are not the ones anybody else would generate.

Description

This library enables you to create a very short string from integer numbers.

The basic usage is:

<?php
$shortid = \ByJG\ShortId\ShortId::fromNumber(9223372036854775807);

// Will write: hWifIaXiv9k

Documentation

Installation

composer require "byjg/shortid"

Quick Start

<?php
use ByJG\ShortId\ShortId;

// Create short ID from number
$shortid = ShortId::fromNumber(1234567890);

// Get the number back
$number = ShortId::get($shortid);

// Create from UUID
$shortid = ShortId::fromUuid('092395A6-BC87-11ED-8CA9-0242AC120002');

// Create from hex
$shortid = ShortId::fromHex('3e');

// Generate random short ID
$shortid = ShortId::fromRandom();

Running Tests

composer test

Open source ByJG