Read each line of txt file to new array element

Dan picture Dan · May 28, 2011 · Viewed 257.6k times · Source

I am trying to read every line of a text file into an array and have each line in a new element.
My code so far.

<?php
$file = fopen("members.txt", "r");
$i = 0;
while (!feof($file)) {

$line_of_text = fgets($file);
$members = explode('\n', $line_of_text);
fclose($file);

?>

Answer

Yanick Rochon picture Yanick Rochon · May 28, 2011

If you don't need any special processing, this should do what you're looking for

$lines = file($filename, FILE_IGNORE_NEW_LINES);