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);

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

  1. 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.

Scroll to Top