簡體   English   中英

在php中生成唯一ID(用於url shortener)

[英]generating unique id's in php (for url shortener)

如何使用(0 - 9),(a - z)和(A - Z)在php中生成最多6個字符的唯一組合? 那將是多少種可能的組合? (例如AAaa與Aaaa不同)?

我的建議(特別是如果您使用數據庫來存儲它們)將是讓數據庫使用數據庫中的自動增量數生成唯一的行ID,然后將該數字轉換為您的代碼,這保證是唯一的,因為它由數據庫生成。

在從數字生成代碼方面,我的建議是一個簡單的替換,所以創建一個包含所有可能字符的字符串,並將您的數字轉換為62(所有字符數)替換每個字母或數字。

AaBbCcDd...0123456789

(另外,我建議刪除lIioO01,因為它們看起來非常相似)

正如Dan Grossman在下面的評論中所建議的那樣,下面的代碼將為您提供一個非常好的近似值。

$code = base_convert($number, 10, 36);

這將根據數字和字母0-9和az給出一個數字。

使用base_convert($number, 10, 36)不要再把az不同於AZ在問題中指定。 自定義功能是必需的。

使用數據庫中的int列作為插入時自動遞增的主鍵,然后在邏輯中將此ID從十進制轉換為base-62(62允許使用0-9,az和AZ)。

創建新的永久鏈接時:

<?php

/**
 * Convert decimal int to a base-62 string
 *
 * @param int $dec
 * @returns string
 */
function toBase62 ($dec) {

  // 0 is always 0
  if ($dec == 0)
    return "0";

  // this array maps decimal keys to our base-62 radix digits
  $values = array(
    "0", "1", "2", "3", "4", 
    "5", "6", "7", "8", "9", 
    "A", "B", "C", "D", "E",
    "F", "G", "H", "I", "J",
    "K", "L", "M", "N", "O",
    "P", "Q", "R", "S", "T",
    "U", "V", "W", "X", "Y", 
    "Z", "a", "b", "c", "d", 
    "e", "f", "g", "h", "i", 
    "j", "k", "l", "m", "n", 
    "o", "p", "q", "r", "s", 
    "t", "u", "v", "w", "x", 
    "y", "z"
  );

  // convert negative numbers to positive.
  $neg = $dec < 0;
  if ($neg)
    $dec = 0 - $dec;

  // do the conversion:
  $chars = array(); // this will store our base-62 chars

  while ($dec > 0) {

    $val = $dec % 62;

    $chars[] = $values[$val];

    $dec -= $val;
    $dec /= 62;

  }

  // add zero-padding:
  while (count($chars) < 6)
    $chars[] = '0';

  // convert to string
  $rv = implode( '' , array_reverse($chars) );

  // if input was negative:
  return $neg ? "-$rv" : $rv;

}


// Usage example:

// ... do mysql insert here and retrieve new insert_id into var $id ...

$permalink = toBase62($id);

?>

解碼請求的永久鏈接時:

<?php

/**
 * Convert base-62 string to a decimal int
 *
 * @param string $str
 * @returns int on success, FALSE on failure
 */
function base62ToInt ($str) {

  // validate str:
  if ( ! preg_match('/^\-?[0-9A-Za-z]+$/', $str) )
    return FALSE; // not a valid string

  // 0 is always 0
  if ($str == "0" )
    return 0;

  // this array maps decimal keys to our base-62 radix digits
  $values = array(
    "0", "1", "2", "3", "4", 
    "5", "6", "7", "8", "9", 
    "A", "B", "C", "D", "E",
    "F", "G", "H", "I", "J",
    "K", "L", "M", "N", "O",
    "P", "Q", "R", "S", "T",
    "U", "V", "W", "X", "Y", 
    "Z", "a", "b", "c", "d", 
    "e", "f", "g", "h", "i", 
    "j", "k", "l", "m", "n", 
    "o", "p", "q", "r", "s", 
    "t", "u", "v", "w", "x", 
    "y", "z"
  );

  // flip $values so it maps base-62 digits to decimal values:
  $values = array_flip($values);

  // get chars from $str:
  $chars = str_split($str);

  // convert negative numbers to positive.
  $neg = $chars[0] == '-';

  if ($neg)
    array_shift($chars);

  // do the conversion:
  $val = 0;
  $i = 0;

  while ( count($chars) > 0 ) {

    $char = array_pop($chars);
    $val += ($values[$char] * pow(62, $i) );
    ++$i;

  }

  return $neg ? 0 - $val : $val;
}


// Usage example:

// ... assuming permalink has been put in a var called $permalink

$id = base62ToInt($permalink);

// ... now look up $id in DB

?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM