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

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

WBOY
WBOYOriginal
2016-06-13 10:00:051086browse

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();

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn