Generate random strings or numeric values
It generates cryptographically secure pseudo-random bytes (using random_bytes()
and random_int()
) to make:
random_bytes()
generatesIf you have Faker installed it falls back to it, giving you access to random names, dates, cities, phones, and a lot more.
Via Composer
$ composer require pragmarx/random
$this->random = new PragmaRX\Random\Random();
$this->random->get(); /// will generate an alpha string which is the same of
$this->random->alpha()->get();
Should give you 16 chars (default size) string
Ajv3ejknLmqwC36z
$this->random->size(32)->get();
$this->random->uppercase()->get();
$this->random->lowercase()->size(255)->get();
To get back to mixed case you can just:
$this->random->mixedcase()->get();
The pattern method uses regex, so you can:
$this->random->pattern('[abcd]')->get();
$this->random->pattern('[A-F0-9]')->get(); /// Hexadecimal
To get
abcddcbabbacbbdabbcb
The pattern method uses regex, so you can:
$this->random->numeric()->start(10)->end(20)->get();
To get
(int) 18
But if you set the size
$this->random->numeric()->size(3)->get();
You’ll get a string
(string) 123
$this->random->hex()->size(10)->get();
Hexadecimal is uppercase by default, but you can get a lowercase by doing:
$this->random->hex()->lowercase()->get();
$this->random->hex()->prefix('#')->size(6)->lowercase()->get();
And you should get a random CSS color:
#fafafa
Of course, the same works for suffixes
$this->random->prefix('!')->suffix('@')->get();
There are currently 43982 questions in the trivia database, and this is how you get them:
$this->random->trivia()->get();
$this->random->trivia()->count(2)->get();
You’ll need to install the Trivia database package:
$ composer require pragmarx/trivia
If you install Faker
composer require fzaninotto/faker
You’ll also have access to all of the Faker features, like:
$this->random->city()->get();
And also use all other features of Random
$this->random->prefix('city: ')->city()->lowercase()->get();
You can also change the faker class, you another one pleases you more:
$this->random->fakerClass(AnotherFaker\Factory::class)->date()->get();
Usually the package returns characters in the range of Base64 (A to Z, a to z and 0 to 9), but you can completely disable this feature and make it return whatever random_bytes()
generates:
$this->random->raw()->get();
Please see CHANGELOG for more information on what has changed recently.
$ composer update
$ vendor/bin/phpunit
Please see CONTRIBUTING for details.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.