PHP生成随机字符串包括大小写字母,这里介绍两种方法:
第一种:利用字符串函数操作
? <?php /** *@blog <www.phpddt.com> */ function createRandomStr( $length ){ $str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' ; //62个字符 $strlen = 62; while ( $length > $strlen ){ $str .= $str ; $strlen += 62; } $str = str_shuffle ( $str ); return substr ( $str ,0, $length ); } echo createRandomStr(10);
第二种:利用数组和字符转换的思想:
? <?php /** *@blog <www.phpddt.com> */ function createRandomStr( $length ){ $str = array_merge (range(0,9),range( 'a' , 'z' ),range( 'A' , 'Z' )); shuffle( $str ); $str = implode( '' , array_slice ( $str ,0, $length )); return $str ; } echo createRandomStr(10);转载于:https://www.cnblogs.com/hgj123/p/3170768.html
相关资源:php产生随机数的两种方法实例代码 输出随机IP