You are here

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

Same name and namespace in other branches
  1. 8 src/SchemaMetatagManager.php \Drupal\schema_metatag\SchemaMetatagManager::randomString()

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 323

Class

SchemaMetatagManager
The SchemaMetatag Manager.

Namespace

Drupal\schema_metatag

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);
}