|
首 页 > php吧 > php随机字符串生成的3种方法 |
人气指数:[ 734 ] |
|
|
本主题共有帖子数 0 篇, [<<] [1] [>>]
|
|
|
| 1 |
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; |
|
|
|
|
|
|
作者:175.166.56.*
2024/6/14 21:06:43 打印 回复
|
|
|
|
|
|
|
|
|
本主题共有帖子数 0 篇, [<<] [1] [>>]
|
|
|
|