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.
asked May 28, 2011 at 4:40
3
If you don't need any special processing, this should do what you're looking for
$lines = file[$filename, FILE_IGNORE_NEW_LINES];
answered May 28, 2011 at 4:48
Yanick RochonYanick Rochon
48.6k24 gold badges122 silver badges195 bronze badges
2
The fastest way that 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 is going to be the path & name of your file, eg. ../filename.txt.
Depending how you've set up your text file, you'll have might have to play around with the \n bit.
Peter Stuart
2,3326 gold badges40 silver badges72 bronze badges
answered Jan 2, 2012 at 19:37
2
Just use this:
$array = explode["\n", file_get_contents['file.txt']];
answered Apr 5, 2013 at 11:15
RoLifeRoLife
4794 silver badges5 bronze badges
0
$yourArray = file["pathToFile.txt", FILE_IGNORE_NEW_LINES];
FILE_IGNORE_NEW_LINES
avoid to add newline at the end of each array element
You can also use FILE_SKIP_EMPTY_LINES
to Skip empty lines
reference here
answered Apr 15, 2015 at 18:46
answered May 28, 2011 at 4:45
GauravGaurav
27.9k8 gold badges48 silver badges78 bronze badges
3
It's just easy as that:
$lines = explode["\n", file_get_contents['foo.txt']];
file_get_contents[]
- gets the whole file as string.
explode["\n"]
- will split the string with the delimiter
"\n"
- what is ASCII-LF escape for a newline.
But pay attention - check that the file has UNIX-Line endings.
If "\n"
will not work properly you have another coding of newline and you can try "\r\n"
, "\r"
or "\025"
answered Mar 21, 2014 at 16:17
CodeBrauerCodeBrauer
2,3551 gold badge23 silver badges48 bronze badges
$lines = array[];
while [[$line = fgets[$file]] !== false]
array_push[$lines, $line];
Obviously, you'll need to create a file handle first and store it in $file
.
answered May 28, 2011 at 4:45
Rafe KettlerRafe Kettler
74.2k19 gold badges152 silver badges149 bronze badges
$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];
answered Dec 1, 2013 at 13:28
0
You were on the right track, but there were some problems with the code you posted. First of all, there was no closing bracket for the while loop. Secondly, $line_of_text would be overwritten with every loop iteration, which is fixed by changing the = to a .= in the loop. Third, you're exploding the literal characters '\n' and not an actual newline; in PHP, single quotes will denote literal characters, but double quotes will actually interpret escaped characters and variables.
answered May 30, 2011 at 5:24
Nick AndrenNick Andren
2042 silver badges4 bronze badges
$file = file["links.txt"];
print_r[$file];
This will be accept the txt file as array. So write anything to the links.txt file [use one line for one element] after, run this page :] your array will be $file
answered Mar 22, 2014 at 22:37
This has been covered here quite well, but if you
REALLY need even better performance than anything listed here, you can use this approach that uses strtok
.
$Names_Keys = [];
$Name = strtok[file_get_contents[$file], "\n"];
while [$Name !== false] {
$Names_Keys[$Name] = 0;
$Name = strtok["\n"];
}
Note, this assumes your file is saved with \n
as the newline character [you can update that as need be], and it also stores the words/names/lines as the array keys instead of the values, so that you can use it as a lookup table, allowing the use of isset
[much, much faster], instead of in_array
.
answered Feb 21, 2018 at 20:59
Brian LeishmanBrian Leishman
7,5209 gold badges55 silver badges86 bronze badges