You are here

static function Util::randomString in X Autoload 7.5

Same name and namespace in other branches
  1. 7.4 lib/Util.php \Drupal\xautoload\Util::randomString()

Generate a random string made of uppercase and lowercase characters and numbers.

Parameters

int $length: Length of the random string to generate

string $chars: Allowed characters

string $chars_first: Allowed characters for the first character.

Return value

string Random string of the specified length

8 calls to Util::randomString()
CacheManager::renewCachePrefix in src/CacheManager/CacheManager.php
Renew the cache prefix, save it, and notify all observers.
Util::randomIdentifier in src/Util.php
Generate a random string that is a valid PHP identifier.
Util::randomProtocol in src/Util.php
Generate a random string that is a stream wrapper protocol.
VirtualFilesystem::__construct in tests/src/Filesystem/VirtualFilesystem.php
xautoload_InjectedAPI_hookXautoload::namespaceHandler in legacy/lib/InjectedAPI/hookXautoload.php
Legacy: Plugins were called "Handler" before.

... See full list

File

src/Util.php, line 23

Class

Util
A number of static methods that don't interact with any global state.

Namespace

Drupal\xautoload

Code

static function randomString($length = 30, $chars = NULL, $chars_first = NULL) {
  if (!isset($chars)) {
    $chars = 'abcdefghijklmnopqrstuvwxyz' . 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' . '1234567890';
  }
  if (!isset($chars_first)) {
    $chars_first = $chars;
  }

  // Initialize the randomizer.
  srand((double) microtime() * 1000000);
  $str = substr($chars_first, rand() % strlen($chars_first), 1);
  for ($i = 0; $i < $length; ++$i) {
    $str .= substr($chars, rand() % strlen($chars), 1);
  }
  return $str;
}