Home > Backend Development > PHP Tutorial > 如何创建这种唯一的KEY

如何创建这种唯一的KEY

WBOY
Release: 2016-06-23 14:04:38
Original
1196 people have browsed it

http://jsfiddle.net/monteslu/ vcKdF/

每新增一条记录,就会创建一个类似“vcKdF”这样的KEY
求这个KEY的算法

谢谢!


回复讨论(解决方案)

52 取 5 的组合(2598960 种,够用了),如果重复了就换一个

<?phpfunction getRandStr($len){         $chars = array("0", "1", "2","3", "4", "5", "6", "7", "8", "9"); //换成a-z,A-Z 52种        $charsLen = count($chars) - 1;         shuffle($chars);        $output = "";        for ($i=0; $i<$len; $i++){             $output .= $chars[mt_rand(0, $charsLen)];         }          return $output;      }?>
Copy after login

function getRandStr($len){	$str = "ABCDEFGHIJKLMNOPQRSTUVWXYZzbcdefghijklmnopqrstuvwxyz";	$output = "";	for ($i=0; $i<$len; $i++){ 		$output .= $str{mt_rand(0, 52)};	}  	return $output;  }
Copy after login


稍微修改了下,谢谢楼上两位

结贴

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template