Read each line of the txt file to a new array element

I am trying to read every line of a text file into an array and every line in a new element. My code is still.

<?php $file = fopen("members.txt", "r"); $i = 0; while (!feof($file)) { $line_of_text = fgets($file); $members = explode('\n', $line_of_text); fclose($file); ?> 
+78
arrays php fgets text-files
May 28 '11 at 4:40 a.m.
source share
10 answers

Unless you need special handling, this should do what you are looking for.

 $lines = file($filename, FILE_IGNORE_NEW_LINES); 
+320
May 28 '11 at 4:48
source share

The fastest way I've found is:

 // Open the file $fp = @fopen($filename, 'r'); // Add each line to an array if ($fp) { $array = explode("\n", fread($fp, filesize($filename))); } 

where $ filename will be empty and your file name, for example ... /filename.txt.

Depending on how you configured your text file, you may have to play with \ n bits.

+29
Jan 02 2018-12-12T00:
source share
 <?php $file = fopen("members.txt", "r"); $members = array(); while (!feof($file)) { $members[] = fgets($file); } fclose($file); var_dump($members); ?> 
+19
May 28 '11 at 4:45
source share

Just use this:

 $array = explode("\n", file_get_contents('file.txt')); 
+19
Apr 05 '13 at 11:15
source share
 $yourArray = file("pathToFile.txt", FILE_IGNORE_NEW_LINES); 

FILE_IGNORE_NEW_LINES avoid adding a new line at the end of each element of the array
You can also use FILE_SKIP_EMPTY_LINES to skip blank lines.

link here

+14
Apr 15 '15 at 18:46
source share
 $lines = array(); while (($line = fgets($file)) !== false) array_push($lines, $line); 

Obviously, you need to first create a file descriptor and save it to $file .

+5
May 28 '11 at 4:45 a.m.
source share

It is just like this:

 $lines = explode("\n", file_get_contents('foo.txt')); 

file_get_contents() - gets the whole file as a string.

explode("\n") - will split a line with a separator "\n" - what is ASCII-LF escape for a new line.

But note - make sure the file has UNIX -Personal endings.

if "\n" does not work properly, you have another 2 new line coding and you can try "\r\n" , "\r" or "\025"

+5
Mar 21 '14 at 16:17
source share
 $file = __DIR__."/file1.txt"; $f = fopen($file, "r"); $array1 = array(); while ( $line = fgets($f, 1000) ) { $nl = mb_strtolower($line,'UTF-8'); $array1[] = $nl; } print_r($array); 
+1
Dec 01 '13 at 13:28
source share

You were on the right track, but there were some problems with the code you posted. First, there was no closing parenthesis for the while loop. Secondly, $ line_of_text will be overwritten with each iteration of the loop, which is fixed by changing in loop = to a. =. Third, you explode the literal characters \ n ', not the actual newline; in PHP, single quotes will indicate alphabetic characters, but double quotes will actually interpret escaped characters and variables.

  <?php $file = fopen("members.txt", "r"); $i = 0; while (!feof($file)) { $line_of_text .= fgets($file); } $members = explode("\n", $line_of_text); fclose($file); print_r($members); ?> 
0
May 30 '11 at 5:24
source share
  $file = file("links.txt"); print_r($file); 

This will take the txt file as an array. So write something in the links.txt file (use one line for one element), run this page :) your array will be $ file

0
Mar 22 '14 at 22:37
source share



All Articles