生成唯一id';php中的s(用于url缩写器)
如何使用(0-9)、(a-z)和(a-z)在php中生成最多6个字符的唯一组合?有多少种可能的组合?(例如AAaa将不同于AAaa)?我的建议(特别是如果您使用数据库存储它们)是让数据库使用数据库中的自动递增编号生成唯一的行id,然后将该编号转换为代码,它保证是唯一的,因为它是由数据库生成的 就从数字生成代码而言,我的建议是一个简单的替换,因此创建一个包含所有可能字符的字符串,并将数字转换为以62为基数(所有字符的计数),每个字符替换一个字母或数字生成唯一id';php中的s(用于url缩写器),php,Php,如何使用(0-9)、(a-z)和(a-z)在php中生成最多6个字符的唯一组合?有多少种可能的组合?(例如AAaa将不同于AAaa)?我的建议(特别是如果您使用数据库存储它们)是让数据库使用数据库中的自动递增编号生成唯一的行id,然后将该编号转换为代码,它保证是唯一的,因为它是由数据库生成的 就从数字生成代码而言,我的建议是一个简单的替换,因此创建一个包含所有可能字符的字符串,并将数字转换为以62为基数(所有字符的计数),每个字符替换一个字母或数字 AaBbCcDd...0123456789
AaBbCcDd...0123456789
(另一方面,我建议删除LIIO01,因为它们看起来非常相似)
正如Dan Grossman在下面的评论中所建议的,下面的代码将为您提供一个非常好的近似值
$code = base_convert($number, 10, 36);
这将根据数字和字母0-9以及a-z为您提供一个数字 使用base\u convert($number,10,36)
处理a-z
与问题中指定的a-z
没有区别。自定义函数是必需的
使用数据库中的int列作为主键,在插入时自动递增,然后在永久链接的逻辑中将此ID从十进制转换为base-62(62允许使用0-9、a-z和a-z)
创建新永久链接时:
<?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
?>
解码请求的永久链接时:
<?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
?>
uniqid()问题不在于生成它们,而在于确保它们保持唯一性。你打算如何存储它们?如果你生成了一个id,你仍然需要每次检查它以确保它以前没有生成过,如果你使用uniquid(),你不必每次都检查。谢谢@戈登,是的,这是我下一个关心的问题,如何在数据库中存储像AA和AA这样的单词,以及如何将它们分开。如果您有任何建议,请告诉我。@Dagon OP正在寻找一个6字符的字符串,而uniqid
将生成一个13字符的字符串。如果你在任何地方截断它,你也必须检查它的唯一性。$code=base\u convert($number,10,36)代码>将使用0-9和a-z。这对他来说应该足够短,这样你就不需要手工编写更高的代码了。