- 相關推薦
PHP生成隨機字符串的技巧
使用PHP開發應用程序,尤其是網站程序,常常需要生成隨機密碼,而本文收集整理了幾種生成隨機字符串的方法,希望對您有所幫助。
例子1:
復制代碼 代碼如下:
< ?php
function genRandomString($len)
{
$chars = array(
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
"S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
"3", "4", "5", "6", "7", "8", "9"
);
$charsLen = count($chars) - 1;
shuffle($chars); // 將數組打亂
$output = "";
for ($i=0; $i<$len; $i++)
{
$output .= $chars[mt_rand(0, $charsLen)];
}
return $output;
}
$str = genRandomString(25);
$str .= "<br />";
$str .= genRandomString(25);
$str .= "<br />";
$str .= genRandomString(25);
echo $str;
?>
例子2:
復制代碼 代碼如下:
<?php
/* Generate Password
* Length : 8
*/
$str = "0123456789abcdefghijklmnopqrstuvwxyz"; // 輸出字符集
$n = 8; // 輸出串長度
$len = strlen($str)-1;
for($j=0 ; $j<200 ; $j++){
for($i=0 ; $i<$n; $i++){
$s .= $str[rand(0,$len)];
}
echo $s . "<br/>";
$s = "";
}
?>
【PHP生成隨機字符串的技巧】相關文章:
PHP簡單生成隨機字符串10-30
PHP生成自定義長度隨機字符串實例07-22
php如何生成隨機密碼07-01
PHP生成隨機密碼的方法11-06
php怎么生成隨機密碼10-29
如何給php生成隨機密碼09-09
使用PHP批量生成隨機用戶名10-17
php生成隨機密碼的幾種方法07-11
php生成N個不重復的隨機數07-27