js
<script language="javascript">
function randomString(len) {
len = len || 32;
var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'; /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
var maxPos = $chars.length;
var pwd = '';
for (i = 0; i < len; i++) {
pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
}
return pwd;
}
document.write(randomString(32));
</script>
php
//随机获取字符
function getRandomStr($length){
$str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';//62个字符
$strlen = 62;
while($length > $strlen){
$str .= $str;
$strlen += 62;
}
$str = str_shuffle($str);
return substr($str,0,$length);
}
$code = getRandomStr(4);
<?php
function getRandStr($length) {
$str = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randString = '';
$len = strlen($str)-1;
for($i = 0;$i < $length;$i ++){
$num = mt_rand(0, $len);
$randString .= $str[$num];
}
return $randString ;
}
$code = getRandStr(4);