PHP file reading question: moving pointer down a line

Megan's picture

She has: 11,421 posts

Joined: Jun 1999

I have a form that writes to a text file. It writes three lines to the file:

a heading
a date
some text

What I want to do is open the file and assign the heading and text to variables ($heading and $message).

This is what I have:

$fp=fopen("../message.txt","r"))
$heading = fgetss($fp);
$message = fgetss($fp);

What this does is sets the $message to the date text, since it's the next line. I could put in a $date variable and assign the next line to that, but it's not needed. What I really want to do is skip to the next line and assign that to $message.

It seems like with fseek you can only move by a certain number of bytes? I haven't found any other way to move the pointer around in a file. Is this possible?

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

For something as simple as that, just get the line and discard it.

You can just call the fgets($fp); without assigning it to anything, or assign it to a dummy variable, or a date variable. (Note, I'm using the fgets, NOT fgetss, as since it is being discarded, no need to also process it to strip tags)

I did a quick check though, didn't see anything. If there was something, it would still pretty much have to do the same functionality (search for the end of the line, move the pointer), just no return the content between current end of line and the new one.

-Greg

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.