Categories
PHP Tips & Tutorials

PHP: Randomizing All Lines of a File – Shuffle Lines in a Text File

Of course, you will have to read in the file first, preferably in an array.

To read a file in an array, you just need the file() function:

$lines = file('quotes.txt');

Then, you shuffle the array with shuffle() function that randomizes all the items in the array thus shuffling the lines in the file quotes.txt:

$lines = shuffle($lines);

By Yang Yang

Hello, I'm Yang. I build online businesses that please people. Want to join in and post some useful articles on Kavoir.com? Shoot me a message.

3 replies on “PHP: Randomizing All Lines of a File – Shuffle Lines in a Text File”

Doesn’t work for me, I have 6,000,000 lines in my text file 🙁
Out of memory, uses more than 500MB
03 Feb 2010

Comments are closed.