从Php中的文本文件中选择一个随机单词?

时间:2011-10-26 20:52:51

标签: php random word text-files

如何从逗号分隔的单词文件中选择一个随机单词?

2 个答案:

答案 0 :(得分:3)

$var = file_get_contents('the_file.txt'); //Take the contents from the file to the variable
$result = explode(',',$var); //Split it by ','
echo = $result[array_rand($result)]; //Return a random entry from the array.

答案 1 :(得分:1)

echo explode(',',file_get_contents('file.txt'))[rand(0,99)];

这是代码量最少的。