php - Select a random value from file while limiting selection to first 5 entries -


i have text file contains 50 lines/entries. want select random value first 5 lines/entries not whole file. example, if file had 7 entries:

1. orange 2. banana 3. apple 4. peach 5. mango 6. berries 7. pineapple 

the output should 1 of first 5 items.

my code far selecting random whole file, not limiting selection.

$filename = file("/home/file.txt");  echo $read_filename = $filename[rand(0, count($$filename) - 1)]; 

you over-complicated it. simply:

<?php $lines = file("/tmp/fruits.txt"); echo $lines[rand(0, 4)]; // display 1 of first 5 lines. 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -