pugx/shortid-php
Composer 安装命令:
composer require pugx/shortid-php
包简介
An implementation of shortid in PHP
README 文档
README
This library is an implementation of ShortId for PHP.
Installation
Install the library via Composer:
composer require pugx/shortid-php
Usage
ShortId is a PHP library that generates short, unique, and random strings. It's useful in scenarios where you need concise identifiers, such as URL shortening or generating unique keys.
Basic usage
Just call PUGX\Shortid\Shortid::generate() to get a random string with default length 7, like "MfiYIvI".
use PUGX\Shortid\Shortid; require_once __DIR__.'/vendor/autoload.php'; $id = Shortid::generate();
Advanced usage
For more control, you can customize the alphabet and length using the Factory class.
The default alphabet uses all letters (lowercase and uppercase), all numbers, underscore, and hyphen.
use PUGX\Shortid\Factory; use PUGX\Shortid\Shortid; require_once __DIR__.'/vendor/autoload.php'; $factory = new Factory(); // alphabet string must be 64 characters long $factory->setAlphabet('é123456789àbcdefghìjklmnòpqrstùvwxyzABCDEFGHIJKLMNOPQRSTUVWX.!@|'); // length must be between 2 and 20 (default is 7) // of course, a lower length increases the clashing probability $factory->setLength(9); Shortid::setFactory($factory); $id = Shortid::generate();
As an alternative, you can customize single generations:
use PUGX\Shortid\Shortid; require_once __DIR__.'/vendor/autoload.php'; $id9 = Shortid::generate(9, 'é123456789àbcdefghìjklmnòpqrstùvwxyzABCDEFGHIJKLMNOPQRSTUVWX.!@|'); $id5 = Shortid::generate(5);
More readable strings
Sometimes, you want to avoid some ambiguous characters, like B/8 or I/l (uppercase/lowercase).
In this case, you can pass a third parameter true to the generate method. Notice that in this case the alphabet
will be ignored, so it makes sense to pass a null one.
Example:
use PUGX\Shortid\Shortid; require_once __DIR__.'/vendor/autoload.php'; $id = Shortid::generate(7, null, true);
Pre-defined values
If you need a deterministic string, instead of a random one, you can call directly the class constructor. This could be useful, for instance, when you need pre-defined data for testing purposes.
use PUGX\Shortid\Shortid; require_once __DIR__.'/vendor/autoload.php'; $myFixedId = new Shortid('5h0r71d'); $anotherFixedId = new Shortid('fooBarZ');
Doctrine
If you want to use ShortId with Doctrine ORM, take a look at ShortId Doctrine type.
Doctrine and Symfony
If you want to use ShortId with Doctrine ORM and Symfony framework, take a look to ShortId Doctrine type bundle.
Dependencies replacement
This library uses a polyfill, so it can be used in environments where the mbstring native extension is not available.
If, instead, your environment is offering that extension, you can avoid installing
that polyfill by configuring a replace entry in your composer.json.
The same applies to the randomLib library: if you are using PHP 8.3 or higher,
you can replace it, since this library uses the native Random class instead.
Contributing
Contributions are welcome. Feel free to open a Pull Request or file an issue here on GitHub! Please read the contributing guidelines first.
pugx/shortid-php 适用场景与选型建议
pugx/shortid-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 619.61k 次下载、GitHub Stars 达 51, 最近一次更新时间为 2015 年 10 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「id」 「uuid」 「shortid」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 pugx/shortid-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 pugx/shortid-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 pugx/shortid-php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Doctrine type for shortid-php
Symfony bundle for ShortId Doctrine type
Shorten and obfuscate your IDs.
DrUUID RFC 4122 library for PHP
Trait to generate uuid when creating models
SilverStripe module for adding an unique id (UUID) to a DataObject
统计信息
- 总下载量: 619.61k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 52
- 点击次数: 22
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-10-30