2011-10-12 58 views
1

我想創建一個簡短的6個字符鏈接與每個提交按鈕命中。如何創建6個字符的固定鏈接?

我的意思是說當一個人點擊提交按鈕時,我想輸入數據並將其保存在數據庫中併爲其創建永久鏈接。它應該是6個字符長,只包含[a-z] [A-Z] [0-9]。

我將使用輸入的數據將6個字符的字符串保存在數據庫中。

那麼我應該如何去做這個代碼,我應該如何生成6個字符?

而且BTW我想在PHP使這個代碼...

謝謝...

+0

可能重複://計算器.com/questions/4977342/generate-unique-ids-in-php-for-url-shortener) – hakre

+0

謝謝你提到的重複...我看到帖子,但我想問一下unuqid()的獨特性。 ?它會永遠是獨一無二的嗎? –

+1

你的意思是:[如何獨特的獨特嗎?](http://stackoverflow.com/questions/4070110/how-unique-is-uniqid) - 你找到右上角的搜索框,如果你錯過了它到目前爲止;) – hakre

回答

1

使用數據庫中的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唯一id的(對於URL縮短)](HTTP的

<?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 (as is "0000...") 
    if (preg_match('/^0+$', $str)) 
    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 

?> 
0

您可以使用固定鏈接複合鍵。 我只是假設你的數據庫表結構。

|Id |Tile          | 
------------------------------------------------- 
|1 | Lorem ipsum dolor sit amet    | 
|2 | consectetur adipiscing elit    | 
|3 | Vestibulum posuere bibendum scelerisque | 

我假設id是自動遞增值。

現在,您可以通過從Id字段中獲取5個字符和一個字符來爲每個條目生成唯一的永久鏈接。 如果您的ID是2位數字,則標題字段爲4個字符,ID等字符爲2個字符。

+0

這是一個非常好的主意,我可以考慮使用它...謝謝... –

+0

如果它的工作,你可以標記我的答案爲接受:) –

+1

如果標題有一個空間在前幾個字符:「示例標題」?如果它有一個數字:id = 1000,title =「12要做的事情」和id = 100012,title =「任何事情」會是一樣的嗎? – megaflop