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...
You can install the package via composer:
composer require stfn/php-random-string
Simple example without any configuration.
$string = RandomString::new()->generate(); // Output: RIKdjFzuDaN12RiJ
You can control the length of the string. By default, it's 16 characters.
$string = RandomString::new(6)->generate(); // Output: dzGcot
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
Or you can use your custom charset for generating random string.
$config = StringConfig::make()
->charset("ABCDEFG1234");
$string = RandomString::fromConfig($config)->generate(); // Output: 3B41B32C2A12A3A1
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
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
RandomString
can generate more than just one string.
$config = StringConfig::make()
->length(6)
->count(3);
$strings = RandomString::fromConfig($config)->generate();
// Output: ["ozBYeT", "BYjCtr", "Sw7O5b"];
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"];
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
composer test
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.
Please see CHANGELOG for more information on what has changed recently.
The MIT License (MIT). Please see License File for more information.