delight-im / random
在PHP中安全生成任何随机内容的最便捷方式
v1.0.0
2019-11-19 23:07 UTC
Requires
- php: >=7.0.0
- delight-im/alphabets: ^1.0
This package is auto-updated.
Last update: 2024-08-24 07:31:40 UTC
README
在PHP中安全生成任何随机内容的最便捷方式
需求
- PHP 7.0.0+
安装
-
通过Composer包含库[?]
$ composer require delight-im/random
-
包含Composer自动加载器
require __DIR__ . '/vendor/autoload.php';
用法
生成整数和自然数
Random::intBetween(300, 999); // => e.g. int(510) // or Random::intBelow(5); // => e.g. int(1) // or Random::intValue(); // => e.g. int(935477113)
生成浮点数(浮点数、双精度或实数)
Random::floatBetween(1, 9); // => e.g. float(7.6388446512546) // or Random::floatBelow(499); // => e.g. float(281.51015805504) // or Random::floatValue(); // => e.g. float(0.05532844200834)
生成布尔值
Random::boolValue(); // => e.g. bool(true)
生成原始字节
Random::bytes(10); // => e.g. "\x58\x3b\x15\x94\x0a\x78\x52\xd0\x53\xb0"
生成UUIDs(版本4)
Random::uuid4(); // => e.g. string(36) "892a24f9-c188-4d06-9885-cf5d8de4592b"
生成十六进制字符串
Random::hexLowercaseString(2); // => e.g. string(2) "5f" // or Random::hexUppercaseString(7); // => e.g. string(7) "B0F4B0C"
生成由字母组成的字符串
Random::alphaString(32); // => e.g. string(32) "GvogcpNdwaxsmOAzKGYwDwqfMOUkZvAn" // or Random::alphaHumanString(7); // => e.g. string(7) "KykrbXb" // or Random::alphaLowercaseString(2); // => e.g. string(2) "ce" // or Random::alphaLowercaseHumanString(32); // => e.g. string(32) "kqbgcmkttvmxjthwpgbhkgdfqhxqmmxh" // or Random::alphaUppercaseString(2); // => e.g. string(2) "IJ" // or Random::alphaUppercaseHumanString(7); // => e.g. string(7) "LJWLLHJ"
生成由字母和数字组成的字符串
Random::alphanumericString(2); // => e.g. string(2) "h9" // or Random::alphanumericHumanString(7); // => e.g. string(7) "NF34gd4" // or Random::alphanumericLowercaseString(2); // => e.g. string(2) "4g" // or Random::alphanumericLowercaseHumanString(32); // => e.g. string(32) "wbdt3fdtg9c33dnxxtphp3qd9tgdvhbn" // or Random::alphanumericUppercaseString(2); // => e.g. string(2) "O7" // or Random::alphanumericUppercaseHumanString(32); // => e.g. string(32) "TWFHNNWLNKMPJMYKXHX3TXRCRFTFMYYW" // or Random::base32String(7); // => e.g. string(7) "AZUELC4" // or Random::base58String(32); // => e.g. string(32) "jut9s2LdWHT1EGJeBug3F58oYsaoU85s"
生成由字母、数字和标点符号组成的字符串
Random::alphanumericAndPunctuationHumanString(32); // => e.g. string(32) "x%jJ7pWTFwc94ctX3phyy^KT~??H4+JY" // or Random::asciiPrintableString(32); // => e.g. string(32) "N~5_ kP5muxf,HeDY2(W_Bwy^qOkgOXa" // or Random::asciiPrintableHumanString(7); // => e.g. string(7) "_r=kb?v" // or Random::base64String(19); // => e.g. string(19) "I7A/H8D2R54uAo6jCQ3" // or Random::base64UrlString(4); // => e.g. string(4) "_6GS" // or Random::base85String(7); // => e.g. string(7) "j8o6sp:"
生成来自任意字符集或字母表的字符串
Random::stringFromAlphabet('abcd+', 7); // => e.g. string(7) "ad+cabb"
将输出字符串分割成段
使用连字符作为分隔符
\Delight\Random\Util::hyphenate('aaaaaaaaaa'); // => string(12) "aaaa-aaaa-aa" // or \Delight\Random\Util::hyphenate('bbbbbbbbbb', 3); // => string(13) "bbb-bbb-bbb-b"
使用空格作为分隔符
\Delight\Random\Util::spaceOut('cccccccccc'); // => string(12) "cccc cccc cc" // or \Delight\Random\Util::spaceOut('dddddddddd', 3); // => string(13) "ddd ddd ddd d"
使用任意分隔符
\Delight\Random\Util::segmentize('eeeeeeeeee', '/'); // => string(12) "eeee/eeee/ee" // or \Delight\Random\Util::segmentize('ffffffffff', '/', 3); // => string(13) "fff/fff/fff/f"
贡献
所有贡献都欢迎!如果您想贡献,请首先创建一个issue,以便讨论您的功能、问题或疑问。
许可
该项目受MIT许可证的条款约束。