Home > php教程 > php手册 > body text

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

WBOY
Release: 2016-06-13 10:00:05
Original
1109 people have browsed it

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

source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template