forever2077/php-random-string
Composer 安装命令:
composer require forever2077/php-random-string
包简介
关键字:
README 文档
README
This package can be used to generate a random string based on your set of characters or predefined ones. You can configure string length, prefix, suffix, and count of strings, or skip some strings under certain conditions...
Installation
You can install the package via composer:
composer require forever2077/php-random-string
Usage
Basic
Simple example without any configuration.
$string = RandomString::new()->generate(); // Output: RIKdjFzuDaN12RiJ
Length definition
You can control the length of the string. By default, it's 16 characters.
$string = RandomString::new(6)->generate(); // Output: dzGcot
Predefined charset
If you want to generate a string consisting of numbers only, lowercase letters, or uppercase letters you can use predefined charsets.
// Generate string that contains only numbers $config = StringConfig::make() ->numbersOnly(); $string = RandomString::fromConfig($config)->generate(); // Output: 9387406871490781 // Generate string that contains only lowercase letters $config = StringConfig::make() ->lowerCaseOnly(); $string = RandomString::fromConfig($config)->generate(); // Output: hvphyfmgnvbbajve // Generate string that contains only uppercase letters $config = StringConfig::make() ->upperCaseOnly(); $string = RandomString::fromConfig($config)->generate(); // Output: ZIVSUDQHAMDNQAYV
Custom charset
Or you can use your custom charset for generating random string.
$config = StringConfig::make() ->charset("ABCDEFG1234"); $string = RandomString::fromConfig($config)->generate(); // Output: 3B41B32C2A12A3A1
Skipping
Sometimes you may want to generate a random string but under certain conditions. For example, give me a string that is not part of this array.
$config = StringConfig::make() ->numbersOnly() ->length(6) ->skip(function ($string) { return in_array($string, ["025922", "104923"]); }); $string = RandomString::fromConfig($config)->generate(); // Output: 083712
Prefix and Suffix
If you want to add a prefix or suffix to generated string, you can do it like this.
$config = StringConfig::make() ->length(6) ->prefix("PRE_") ->suffix("_AFTER"); $string = RandomString::fromConfig($config)->generate(); // Output: PRE_rkM7Jl_AFTER
Array of random strings
RandomString can generate more than just one string.
$config = StringConfig::make() ->length(6) ->count(3); $strings = RandomString::fromConfig($config)->generate(); // Output: ["ozBYeT", "BYjCtr", "Sw7O5b"];
Uniqueness
It may happen (rarely, but it's possible) to have not unique strings in the generated array. If you want to avoid it, just change the config.
$config = StringConfig::make() ->length(6) ->count(3) ->unique(); $strings = RandomString::fromConfig($config)->generate(); // Output: ["92ONRj", "Me6oym", "WbBPVc"];
Everything in one line
You can use the fromArray method if you don't want to create 2 objects every time.
$string = RandomString::fromArray(['length' => 6, 'charset' => 'ABCD1234'])->generate(); // Output: CCDA1D
Testing
composer test
Security
While the RandomString class is designed to generate random and unpredictable string, it is important to note that it is not a cryptographically secure hash function and should not be used for sensitive applications such as password hashing or cryptographic key generation.
Changelog
Please see CHANGELOG for more information on what has changed recently.
Credits
License
The MIT License (MIT). Please see License File for more information.
forever2077/php-random-string 适用场景与选型建议
forever2077/php-random-string 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 23 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 08 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「random-string」 「stfn」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 forever2077/php-random-string 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 forever2077/php-random-string 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 forever2077/php-random-string 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
统计信息
- 总下载量: 23
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 3
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-08-20