首页  >  文章  >  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