You are here

function shurly_generate_random in ShURLy 6

Same name and namespace in other branches
  1. 8 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 736
description http://www.youtube.com/watch?v=Qo7qoonzTCE

Code

function shurly_generate_random($len = NULL) {
  if ($len == NULL) {
    $len = variable_get('shurly_length', 4);
  }
  $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));
  return $str;
}