<!DOCTYPE html>
<html>
<head>
<title>Browse Directories</title>
<meta charset="utf-8">
</head>
<body>
<p>从字典中获取一个随机单词,以生成新的密码</p>
<?php
function get_random_word($min_length, $max_length)
{
$word = '';
$dictionary = '字典路径';
$fp = @fopen($dictionary, 'r');
if(!$fp)
{
echo '不能打开文件';
return false;
}
$size = filesize($dictionary);
$rand_location = rand(0, $size);
fseek($fp, $rand_location);
//获取一个长度介于$min_length和$max_length之间,并且不包含'
while((strlen($word) < $min_length) || (strlen($word) > $max_length) || (strstr($word, "'")))
{
if(feof($fp))//如果查找单词的文件尾,就从头开始
{
fseek($fp, 0);
}
$word = fgets($fp, 80);//因为第一次可能获取到单词的一部分,所以使用两次fgets
$word = fgets($fp, 80);
}
$word = trim($word);
return $word;
}
echo get_random_word(7, 16);
?>
</body>
</html>
词典上传路径https://sourceforge.net/projects/wordlist/?source=typ_redirect