PHP code example of district5 / random

1. Go to this page and download the library: Download district5/random library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

district5 / random example snippets


$
// fast UUID generator
$uuid = \District5\Random\Uuid::simple($ \District5\Random\Uuid::highEntropy($cost, $

$length = 16;

// generate an alphanumeric string
$str = \District5\Random\Strings::alphanumeric($length);

// generates an alphabetical lowercase string
$str = \District5\Random\Strings::alphabeticLowercase($length);

// generates a hex string
$str = \District5\Random\Strings::hex($length);

// generates a string using a set of allowable characters
$str = \District5\Random\Strings::fromStringOfAllowableCharacters('abcd1234', $length);

// generates a string with full custom control
$allowAlphabetical = true;
$allowNumeric = true;
$allowSpecial = true;
$excludeAmbiguous = false;
$toIgnore = ['1', 'i', 'I', '0', 'O', 'o'];
$forceEachType = true;
$str = \District5\Random\Strings::custom(
    $length,
    $allowAlphabetical,
    $allowNumeric,
    $allowSpecial,
    $excludeAmbiguous,
    $ignoreCharacters,
    $forceEachType
);

// generate an 8 character single password with allowed characters including alphabetical, numerical and special
$password = \District5\Random\Password::single(8);

// generate a list of 5 x 8 character passwords
$passwords = \District5\Random\Password::multiple(5, 8);

// Between 0 and 10
$int = \District5\Random\Integer::inRange(0, 10);

// generate an integer between 1 and mt_getrandmax()
$int = \District5\Random\Integer::positive();

// generate an integer between 1 and 55
$int = \District5\Random\Integer::positive(55);

// generate an integer between -1 and negative mt_getrandmax()
$int = \District5\Random\Integer::negative();

// generate an integer between -1 and -55
$int = \District5\Random\Integer::negative(-55);

$data = ['foo', 'bar', 'dog', 'cat'];
$key = \District5\Random\Arrays::randomKey($data);
// $key would be in the range of 0-3 inclusive

$data = ['a' => 'foo', 'b' => 'bar', 'c' => 'dog', 'd' => 'cat'];
$value = \District5\Random\Arrays::randomItem($data);
// $value would be one of foo|bar|dog|cat

// generates a pseudo random DateTime between the epoch and now
$randomDateTime = \District5\Random\Dates::dateTime();

// generates a pseudo random DateTime between DateTime 1 and DateTime 2.
$randomDateTime = \District5\Random\Dates::dateTimeBetweenDateTimes($dt1, $dt2);

// generate a pseudo random date string between the epoch and now with default formatting of Y-m-d (optional argument defaulted to Y-m-d)
$randomDateStr = \District5\Random\Dates::date('Y-m-d');

// generates a pseudo random date string between DateTime 1 and DateTime 2.
$randomDateStr = \District5\Random\Dates::dateBetweenDateTimes($dt1, $dt2, 'Y-m-d');