Download the PHP package linkorb/xuid without Composer
On this page you can find all versions of the php package linkorb/xuid. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package xuid
Short Description XUID library for PHP
License MIT
Homepage https://github.com/linkorb/xuid
Informations about the package xuid
XUID Library
XUIDs are URL-friendly compressed UUIDs www.xuid.org
Why XUIDs?
A UUID v4 is a great choice as a primary key for your database tables.
But, they are quite long to use in URLs and databases : 32 alphanumeric characters and four hyphens (36 characters total)
A XUID is a UUID, converted into a 128-bit value, converted into a base64 string (stripped from padding characters), then converted into url-safe base64 (replacing +
and /
into -
and _
respectively - custom mappings available, see below for more info).
This gives you a 22 character string, safe to use in URLs.
It also safely decodes into a full UUID string again.
Benefits
- Takes less space in database fields
- Shorter URLs
- Un-guessable primary keys in URLs
Usage
All methods can be called statically as well:
Example
You'll find example code in the example/
directory.
To generate 100 XUIDs, run the following command:
Example output:
Generatic alpha-numeric XUIDs only
-
and _
are occassionally found in XUIDs. If you prefer alpha-numeric only XUIDs, simply call the following static method before generating your XUIDs:
This will keep generating XUIDs until one is found that only contains alphanumeric characters. That means that it could require two or more attempts before a valid XUID can be returned. In practice this does not add any noticable latency. But this is worth keeping in mind when generating XUIDs in bulk.
Custom character mappings
The default character mapping for XUIDs (url safe) is:
+
=>-
/
=>_
But you can apply any other mapping you want for any of the characters used in base64 encoding, by calling Xuid::map()
. For example, you can create double-click safe XUIDs (the whole XUID is selectable simply by double clicking it), by mapping +
and _
to selectable characters. For example:
Beware to only pass in URL safe characters if your use-case requires URL safe XUIDs.
PHPUnit tests
Brought to you by the LinkORB Engineering team
Check out our other projects at engineering.linkorb.com.
Btw, we're hiring!