byjg / shortid
从数字创建短字符串ID
4.9.0
2023-05-21 22:24 UTC
Requires
- php: >=5.6.0
Requires (Dev)
- phpunit/phpunit: 5.7.*|7.4.*|^9.5
This package is auto-updated.
Last update: 2024-09-16 05:20:24 UTC
README
从数字创建短字符串ID
描述
此库允许您从整数数字创建一个非常短的字符串。
基本用法是
<?php $shortid = \ByJG\Utils\ShortId::fromNumber(81717788171667188198); // Will write: Qi0yuM2uKwJb
方法
- fromNumber($number, $map = null): 从数字返回一个短ID
- fromHex($hex, $map = null): 从十六进制值返回一个短ID
- fromUuid($uuid, $map = null): 从UUID返回一个短ID
- fromRandom($min, $max, $map = null): 从随机数返回一个短ID
- get($shortid, $map = null): 从短ID返回唯一的整数数字(不适用于UUIDs)
指定您自己的映射
短ID的基数在映射定义中。
基本上,您可以在其中创建自己的序列。
库默认定义了四个
- $MAP_DEFAULT
- $MAP_ALTERNATE
- $MAP_NUMBERS_FIRST
- $MAP_RANDOM
基本用法是
<?php $shortid = \ByJG\Utils\ShortId::fromNumber( 81717788171667188198, \ByJG\Utils\ShortId::$MAP_NUMBERS_FIRST ); // Will write: G8QokCSkAmz1
安装
composer require "byjg/php-shortid"
测试
vendor/bin/phpunit