1.相关的随机函数:
mt_rand(),shuffle(),str_shuffle()
2.总之就是字符串 数组 的灵活使用
#打乱字符串str_shuffle, 截取字符串substr
$str="abcdefghjklmnuvwxyz0123456789"; $str = str_shuffle($str); echo substr($str,0,8);
#看别人写的随机生成串的函数
产生字符串的长度-》随机得到数据的小标》取数组的值拼接就ok
public static function make_rand_string($len, $include_number = false) { $string_list = array (‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘, ‘J‘, ‘K‘, ‘L‘, ‘M‘, ‘N‘, ‘P‘, ‘Q‘, ‘R‘, ‘S‘, ‘T‘, ‘U‘, ‘V‘, ‘W‘, ‘X‘, ‘Y‘, ‘Z‘, ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘, ‘h‘, ‘j‘, ‘k‘, ‘m‘, ‘n‘, ‘p‘, ‘q‘, ‘r‘, ‘s‘, ‘t‘, ‘u‘, ‘v‘, ‘w‘, ‘x‘, ‘y‘, ‘z‘ ); $string_include_number_list = array (‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘, ‘J‘, ‘K‘, ‘M‘, ‘N‘, ‘P‘, ‘Q‘, ‘R‘, ‘S‘, ‘T‘, ‘U‘, ‘V‘, ‘W‘, ‘X‘, ‘Y‘, ‘Z‘, ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘, ‘h‘, ‘j‘, ‘k‘, ‘m‘, ‘n‘, ‘p‘, ‘q‘, ‘r‘, ‘s‘, ‘t‘, ‘u‘, ‘v‘, ‘w‘, ‘x‘, ‘y‘, ‘z‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘ ); if ($include_number) { $arr = $string_include_number_list; } else { $arr = $string_list; } $rand_max = count ( $arr ) - 1; $return_string = null; for($i = 0; $i < $len; $i ++) { $rand = mt_rand ( 0, $rand_max ); $return_string .= $arr [$rand]; } return $return_string; }
时间: 2024-10-15 07:09:33