n00bl3t321 n00bl3t321 - 6 months ago 17
PHP Question

How to create a php script that echo'es from txt document

I am not really a coder... and I have search around about this small script...

However, I only found a script that will echo the entire content of a file and not by line.

Can you guys help me write a small script that will echo or print out

a random line from a text file (by url - www.mydomain.com/myfile.txt)

the myfile.txt will have thousands of lines
ie:

random-text here 1
random-text here 2
random-text here 3
random-text here 4
random-text here 5


I only want to randomly echo a line... for example:
only echo


random-text here 4


Thank you so much in advance!

Answer

You can use file(), i.e..

$lines = file('yourfile.txt');
echo $lines[4];

if you want to echo a random line, use rand, i.e.:

$lines = file('yourfile.txt');
echo $lines[rand(0, count($lines)) -1];

file()

file — Reads entire file into an array

Comments