WireRandom::alphanumeric()

Return random alphanumeric, alpha or numeric string

This method uses cryptographically secure random generation unless you specify true for the fast option, in which case it will use cryptographically secure method only if PHP is version 7+ or the mcrypt library is available.

Note about the allow option: If this option is used, it overrides the alpha and numeric options and creates a string that has only the given characters. If given characters are not ASCII alpha or numeric, then the fast option is always used, as the crypto-secure option does not support non-alphanumeric characters. When the allow option is used, the strict option does not apply.

Available since version 3.0.111.

Usage

// basic usage
$string = $wireRandom->alphanumeric();

// usage with all arguments
$string = $wireRandom->alphanumeric(int $length = 0, array $options = []);

Arguments

NameType(s)Description
$length (optional)int

Required length of string, or 0 for random length

$options (optional)array

Options to modify default behavior:

  • alpha (bool): Allow ASCII alphabetic characters? Default:true
  • upper (bool): Allow uppercase ASCII alphabetic characters? Default:true
  • lower (bool): Allow lowercase ASCII alphabetic characters? Default:true
  • numeric (bool): Allow numeric characters 0123456789? Default:true
  • strict (bool): Require that at least 1 character representing each true option above is present? Default:false
  • allow (array|string): Only allow these ASCII alpha or digit characters, see notes. Default:''
  • disallow (array|string): Do not allow these characters. Default:''
  • require (array|string): Require that these character(s) are present. Default:''
  • extras (array|string): Also allow these non-alphanumeric extra characters. Default:''
  • minLength (int): If $length argument is 0, minimum length of returned string. Default:10
  • maxLength (int): If $length argument is 0, maximum length of returned string. Default:40
  • noRepeat (bool): Prevent same character from appearing more than once in sequence? Default:false
  • noStart (string|array): Do not start string with these characters. Default:''
  • 'noEnd` (string|array): Do not end string with these characters. Default:''
  • fast (bool): Use faster method? Default:true if PHP7 or mcrypt available, false if not

Return value

string

Exceptions

Method can throw exceptions on error:

  • WireException


WireRandom methods and properties

API reference based on ProcessWire core version 3.0.251