/** * php 生成随机字符 * @param number $length 长度 * @param string $type 类型 * @param number $convert 转换大小写 * @return string */ function random($length=6, $type='string', $convert=0){ $config = array( 'number'=>'1234567890', 'letter'=>'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 'string'=>'abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789', 'all'=>'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890' ); if(!isset($config[$type])) $type = 'string'; $string = $config[$type]; $code = ''; $strlen = strlen($string) -1; for($i = 0; $i < $length; $i++){ $code .= $string{mt_rand(0, $strlen)}; } if(!empty($convert)){ $code = ($convert > 0)? strtoupper($code) : strtolower($code); } return $code; }
使用方法见代码顶部的注释。
本文为原创文章,版权归主机之家测评所有,欢迎分享本文,转载请保留出处!
你可能也喜欢
- ♥ apache:configuration error: couldn't perform authentication. AuthType not set!: /08/26
- ♥ thinkphp 实现文件下载08/21
- ♥ 【疯狂猜成语/图猜成语】两个人在说话前面有一个人在笑是什么成语?08/19
- ♥ 宝塔面板部署 Laravel 提示:The Process class relies on proc_open, which is not available on your PHP installation.07/06
- ♥ Uncaught SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode 错误的解决办法08/28
- ♥ Debian 系统下宝塔面板安装 Nginx 后不显示的解决办法08/28