Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text file into an array?

Tags:

php

In php how can I read a text file and get each line into an array?

I found this code which does it, somewhat, but looks for a = sign and I need to look for a new line:

<?PHP
$file_handle = fopen("dictionary.txt", "rb");
while (!feof($file_handle) ) {
  $line_of_text = fgets($file_handle);
  $parts = explode('=', $line_of_text);
  print $parts[0] . $parts[1]. "<BR>";
}
fclose($file_handle);
?>
like image 694
JasonDavis Avatar asked Apr 09 '26 11:04

JasonDavis


1 Answers

Well, you could just replace the '=' with a "\n" if the only difference is that you're looking for a newline.

However, a more direct way would be to use the file() function:

$lines = file("dictionary.txt");

That's all there is to it!

like image 107
VoteyDisciple Avatar answered Apr 11 '26 01:04

VoteyDisciple