You are here

function shurly_generate_random in ShURLy 8

Same name and namespace in other branches
  1. 6 shurly.module \shurly_generate_random()
  2. 7 shurly.module \shurly_generate_random()

Generate a random short URL Pretty much unused at this point this method could take a LOOOONG time on a site with lots of URLs

File

./shurly.module, line 493
Description http://www.youtube.com/watch?v=Qo7qoonzTCE.

Code

function shurly_generate_random($len = NULL) {
  if ($len == NULL) {
    $len = \Drupal::config('shurly.settings')
      ->get('shurly_length');
  }
  $charset = "abcdefghijklmnopqrstuvwxyz123456789";
  $charlen = strlen($charset) - 1;
  do {
    $str = '';
    for ($i = 0; $i < $len; $i++) {
      $str .= $charset[mt_rand(0, $charlen)];
    }

    // Check that this string hasn't been used already
    // check that the string is a valid (available) path.
  } while (shurly_url_exists($str) || !shurly_path_available($str));

  // Allow extra operations.
  \Drupal::moduleHandler()
    ->invokeAll('shurly_shorturl_extra', [
    $str,
  ]);
  return $str;
}