You are here

public function GdprDumpUtilRandom::name in General Data Protection Regulation 7

Generates a random string containing letters and numbers.

The string will always start with a letter. The letters may be upper or lower case. This method is better for restricted inputs that do not accept certain characters. For example, when testing input fields that require machine readable values (i.e. without spaces and non-standard characters) this method is best.

Parameters

int $length: Length of random string to generate.

Return value

string Randomly generated string.

See also

GdprDumpUtilRandom::string()

File

modules/gdpr_dump/src/GdprDumpUtilRandom.php, line 50

Class

GdprDumpUtilRandom
Defines a utility class for creating random data.

Code

public function name($length = 8) {
  $values = array_merge(range(65, 90), range(97, 122), range(48, 57));
  $max = count($values) - 1;
  $str = chr(mt_rand(97, 122));
  for ($i = 1; $i < $length; $i++) {
    $str .= chr($values[mt_rand(0, $max)]);
  }
  return $str;
}