老师,你之前不是已经对长度做了控制了吗?substr($chars,0,$length) 但是之后你写的FOR的作用是什么呢?
6 回答
function buildRandomString($type = 1, $length = 4){ if ($type == 1) { $chars = join('', range(0, 9)); }elseif ($type == 2) { $chars = join(array_merge(range('a', 'z'), range('A', 'Z'))); }elseif ($type == 3) { $chars = join(array_merge(range('a', 'z'), range('A', 'Z'), range(0, 9))); } if ($length > strlen($chars) { exit('验证码设定的长度过长'); } $chars = str_shuffle($chars); return substr($chars, 0, $length); }
我有个地方写错了,今天改过来.
我猜你是说string.func.php里的return substr($chars, 0, $length); 是吧?
function buildRandomString($type = 1, $length = 4){ if ($type == 1) { $chars = join('', range(0, 9)); }elseif ($type == 2) { $chars = join(array_merge(range('a', 'z'), range('A', 'Z'))); }elseif ($type == 3) { $chars = join(array_merge(range('a', 'z'), range('A', 'Z'), range(0, 9))); } if ($length > $chars) { exit('验证码设定的长度过长'); } $chars = str_shuffle($chars); return substr($chars, 0, $length); }
调用buildRandomString()函数时,如果你不传值,$type默认是1, $length默认是 4。如果你传$length=80,也就是你要生成一个80位的验证码,我们看看行不行?
$chars = join(array_merge(range('a', 'z'), range('A', 'Z'), range(0, 9)));此时,$chars生成的是一个26+26+10=62位的字符串(这是这三种$type中最长的一个了)。你想从这62位的字符串中截取前80位?这是不可能的。就像是你养了头200斤的猪,你要从中割200斤肉的肉食一个道理的。
举报
0/150
提交
取消