php - efficent way to get and remove first line in file
Posted
by Marco Demaio
on Stack Overflow
See other posts from Stack Overflow
or by Marco Demaio
Published on 2010-03-08T21:02:22Z
Indexed on
2010/03/08
21:06 UTC
Read the original article
Hit count: 202
Hello,
I have a script that each time is called gets the 1st line of a file. Each line is known to be exactly of the same length (32 alphanumerci chars) and terminates with a "\r\n". After getting the 1st line, the script removes it.
Now I do in this way:
$contents = file_get_contents($file));
$first_line = substr($contents, 0, 32);
file_put_contents($file, substr($contents, 32 + 2)); //+2 because we remove also the \r\n
Obvioulsy it works, but I was wondering if there could be a smarter (or more efficent) way to do this??? In my simple solution I basically read and rewrite all the file just to take and remove the 1st line.
Thanks!
© Stack Overflow or respective owner