php随机字符串生成的3种方法
方法一:
$str = abcdefg;
$length = strlen($str);
$shuffledStr = ;
while ($length > 0) {
$index = mt_rand(0, $length - 1);
$shuffledStr .= $str[$index];
$str = substr($str, 0, $index) . substr($str, $index + 1);
$length--;
}
echo $shuffledStr;
方法二:
$str = abcdefg;
$array = str_split($str);
shuffle($array);
$shuffledStr = implode(, $array);
echo $shuffledStr;
方法三:
$str = abcdefg;
$array = str_split($str);
$shuffledStr = ;
while (!empty($array)) {
$index = array_rand($array);
$shuffledStr .= $array[$index];
unset($array[$index]);
}
echo $shuffledStr;