首頁  >  文章  >  php教程  >  php生成字符串随机码实现方法

php生成字符串随机码实现方法

WBOY
WBOY原創
2016-06-13 10:00:051113瀏覽

php教程生成字符串随机码实现方法


*/
$validcharacters = "abcdefghijklmnopqrstuxyvwzabcdefghijklmnopqrstuxyvwz+-*#&@!?";
$index = mt_rand(0, 10);
echo $index;

//看一款复杂一点的

$validcharacters = "abcdefghijklmnopqrstuxyvwzabcdefghijklmnopqrstuxyvwz+-*#&@!?";
$validcharnumber = strlen($validcharacters);
$index = mt_rand(0, $validcharnumber-1);
$randomcharacter = $validcharacters[$index];
echo $randomcharacter;

//实例二

$length = 5;
$result = "";
for ($i = 0; $i  $index = mt_rand(0, $validcharnumber-1);   
 $result .= $validcharacters[$index];
}

//

function getrandomstring($length = 6) {   
 $validcharacters = "abcdefghijklmnopqrstuxyvwzabcdefghijklmnopqrstuxyvwz+-*#&@!?";   
 $validcharnumber = strlen($validcharacters);    
 $result = "";    
 for ($i = 0; $i   $index = mt_rand(0, $validcharnumber - 1);       
  $result .= $validcharacters[$index];   
 }    
 return $result;
}
echo getrandomstring();

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn