first line of file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

first line of file

Post by Illusionist »

How can i get the first line in a text file?
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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.
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

actually neither of those functions do anything even close to what i need. Did you actually read what i asked?
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

Fixed my problem. Don't need to worry about multiple lines now because i changed my method of writing to the file!!
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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?
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Post 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?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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];
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

You could also do :

Code: Select all

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