Page 1 of 1

first line of file

Posted: Thu Feb 26, 2004 8:36 pm
by Illusionist
How can i get the first line in a text file?

Posted: Thu Feb 26, 2004 8:56 pm
by tim
you will want to review on the function: rewind()

php is awesome with these sorts of functions, as ftell() enables u to insert the "file pointer" to whatever line you want....

kudos.

Posted: Thu Feb 26, 2004 9:03 pm
by Illusionist
actually neither of those functions do anything even close to what i need. Did you actually read what i asked?

Posted: Thu Feb 26, 2004 9:25 pm
by Illusionist
Fixed my problem. Don't need to worry about multiple lines now because i changed my method of writing to the file!!

Posted: Thu Feb 26, 2004 9:28 pm
by tim
i'm sorry... for some reason I thought u wanted to know how to insert the file pointer...

I would have no idea how to actually get the actual "first" line of text...
i'm now curious on how so if anyone knows, post... somehow put the textfile data into a array and read the array as such somehow?

Posted: Fri Feb 27, 2004 8:28 am
by liljester
fgets($file_handler) will return a single line at a time, and if the file is freshly opened, it will get the first line. but if you've already moved the file pointer around a bit, rewind($file_handler) is exaclty what you need to go back to the beginning of the file.

did you mean how to put the first line into a file, mabe?

Posted: Fri Feb 27, 2004 9:51 am
by pickle

Code: Select all

$file_array = file('file_name_here');
will read in each line of the file into an element of the array, to access the first line, just read:

Code: Select all

$first_line = $file_array[0];

Posted: Fri Feb 27, 2004 10:01 am
by markl999
You could also do :

Code: Select all

echo end(array_reverse(file('test.txt')));