Skipping lines with file_get_contents?

I try to skip the first 2 lines (from reading 3 files) and then save back (I already did this, all that remains is a line skip)

Is there any way to do this?

+3
source share
4 answers

This is one way to do this. Perhaps this was a bit overdone, since it is not very effective. (use file()will be much faster)

$content = file_get_contents($filename);
$lines = explode("\n", $content);
$skipped_content = implode("\n", array_slice($lines, 2));
+6
source

Yes, but using file_get_contents would be too complicated. I recommend using instead file():

$file_array = file("yourfile.txt");
unset($file_array[0]);
unset($file_array[1]);
file_put_contents("outfile.txt", implode("", $file_array));
+1
source

(), 2 ,

0

If the lines are not very long, can you just use a regular expression to read files? From the php directory, the file_get_contents file has an offset parameter, although this is most likely not to be useful, since you need to know the length of the line in advance. Maybe file_get_contents is not a suitable function to use in this case?

0
source

Source: https://habr.com/ru/post/1792322/


All Articles