/**
* @param string $str 生成验证码的字符
* @param int $len
* @param string $format
* @param string $salt
* @return bool|string
*/
function genCode($str, $len = 4, $format = 'NUMBER', $salt = '')
{
$salt = $salt ? $salt : 'secret string';
if (!$str) {
return false;
}
$_str = $str . $salt;
$_str = trim(base64_encode(md5($_str)), '=');
switch ($format) {
case 'ALL':
$chars = preg_replace('/0|o|O/', '', $_str);
break;
case 'CHAR':
$chars = preg_replace('/\d|o|O/', '', $_str);
break;
case 'NUMBER':
default:
$chars = base_convert($_str, 36, 10);
$chars = str_replace('0', '', $chars);
break;
}
return substr($chars, 0, $len);
}
网友评论