php 自定义验证码函数的问题
请教各位一个问题:
php 自定义验证码函数
<?php
function random_text($count, $rm_similar = false)
{
$chars = array_flip(array_merge(range(0, 9), range('A', 'Z')));
if ($rm_similar)
{
unset($chars[0], $chars[1], $chars[2], $chars[5], $chars[8],
$chars['B'], $chars['I'], $chars['O'], $chars['Q'],
$chars['S'], $chars['U'], $chars['V'], $chars['Z']);
}
for ($i = 0, $text = ''; $i < $count; $i++)
{
$text .= array_rand($chars);
}
return $text;
}
?>
这段代码是什么作用了,$chars[5]这些是啥意思:unset($chars[0], $chars[1], $chars[2], $chars[5], $chars[8],
$chars['B'], $chars['I'], $chars['O'], $chars['Q'],
$chars['S'], $chars['U'], $chars['V'], $chars['Z']);
这个函数调用的话,怎么调用了,给个例子。
------解决方案--------------------<?php
function random_text($count, $rm_similar = false)
{
$chars = array_flip(array_merge(range(0, 9), range('A', 'Z')));//生成一个二维数组[0-9][A-Z]数组的值依次递增 比如['A']=10,['B']=11 ....
if ($rm_similar)
{
unset($chars[0], $chars[1], $chars[2], $chars[5], $chars[8],
$chars['B'], $chars['I'], $chars['O'], $chars['Q'],
$chars['S'], $chars['U'], $chars['V'], $chars['Z']);
//根据参数$rm_similar(默认为false)来判断是否释放上面这些数组的值
}
for ($i = 0, $text = ''; $i < $count; $i++)
{
$text .= array_rand($chars);//根据$count来随机取的数组中的值
}
return $text;
}
?>
示例:
//1.取7位文本代码,并去除unset里的值
random_text(7,true);
//2.取5位文本代码,不去除unset里的值,这里第二个参数按函数默认的来即为false
random_text(5);