How can I pick a random word from a file of comma-separated words?
Asked
Active
Viewed 4,311 times
2
-
@Jesse you have posted a link to this same page. – mickmackusa Aug 27 '22 at 00:05
-
Related: https://stackoverflow.com/questions/306400/how-can-i-randomly-select-an-item-from-a-list – Jesse Nickles Aug 27 '22 at 04:11
2 Answers
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.

Madara's Ghost
- 172,118
- 50
- 264
- 308
1
echo explode(',',file_get_contents('file.txt'))[rand(0,99)];
That is it in the least amount of code.

Chris
- 1,569
- 2
- 11
- 18