You are here

public static function SchemaMetatagManager::randomString in Schema.org Metatag 7

Generates a pseudo-random string of ASCII characters of codes 32 to 126.

Parameters

int $length: Length of random string to generate.

Return value

string Pseudo-randomly generated unique string including special characters.

Overrides SchemaMetatagManagerInterface::randomString

File

src/SchemaMetatagManager.php, line 335
A generic substitution for Drupal 8 Random utility.

Class

SchemaMetatagManager
Class SchemaMetatagManager.

Code

public static function randomString($length = 8) {
  $randomGenerator = new Random();
  if ($length < 4) {
    return $randomGenerator
      ->string($length, TRUE);
  }

  // Swap special characters into the string.
  $replacement_pos = floor($length / 2);
  $string = $randomGenerator
    ->string($length - 2, TRUE);
  return substr_replace($string, '>&', $replacement_pos, 0);
}