I have a .txt file which I am reading with php (fread). I want to print each line separately (so I can write a function to print only specified lines). How do I do this? How can I read this file line by line?
Also, if I have a delimiter, how can I put each field between the delimiters in its own array?
Any help on this will be much, much appreciated! Thanks!
If you use file() to read the file, then you will have an array of lines which you can loop through (via foreach()) or call specific line via the array index.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
If you use file() to read the file, then you will have an array of lines which you can loop through (via foreach()) or call specific line via the array index.
I agree. This is the way to do it if you are trying to read a file "line by line" since file() automatically splits on line returns. It can really help to simplify your code since you would not have to fopen() or fclose() the file.
fread() would be more appropriate if you were trying to read a file by bytes or some other increment.
as far as the delimeters to get the fields to another array, uisng the explode() function within the foreach loop mentioned above may help facilitate that.
Bookmarks