如何使用 (0 - 9)、(a - z) 和 (A - Z) 在 php 中生成最多 6 个字符的唯一组合?有多少可能的组合?(例如 AAaa 将与 Aaaa 不同)?
使用base_convert($number, 10, 36)不会与问题中指定的a-z不同A-Z。需要自定义函数。
使用数据库中的 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);
?>
Run Code Online (Sandbox Code Playgroud)
解码请求的永久链接时:
<?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
?>
Run Code Online (Sandbox Code Playgroud)
我的建议(特别是如果您无论如何都使用数据库来存储它们)是让数据库使用数据库中的自动增量数字生成唯一的行ID,然后将该数字转换为您的代码,这保证是唯一的,因为它由数据库生成。
就从数字生成代码而言,我的建议是进行简单的替换,因此创建一个包含所有可能字符的字符串,并将数字转换为基数 62(所有字符的计数),用字母或数字替换每个 。
AaBbCcDd...0123456789
Run Code Online (Sandbox Code Playgroud)
(另一方面,我建议删除 lIioO01,因为它们看起来都非常相似)
正如 Dan Grossman 在下面的评论中所建议的,以下代码将为您提供非常接近您想要的内容。
$code = base_convert($number, 10, 36);
Run Code Online (Sandbox Code Playgroud)
这将为您提供一个基于数字和字母 0-9 以及 az 的数字。