ktsirig Posted September 27, 2009 Share Posted September 27, 2009 Hello all! Is there a way to remove the last line in a text file? Or, alternatively, is there a way to read a text file until the penultimate line of the file? That is, if you have a file like: AAAA BBBB CCCC DDDD XXXXXXXX read all lines except the last one? In the previous example I need to hold only AAAA, BBBB , CCCC and DDDD. Thank you! Link to comment https://forums.phpfreaks.com/topic/175739-remove-last-line-in-a-file-phpstore-all-lines-except-the-last-one/ Share on other sites More sharing options...
Alex Posted September 27, 2009 Share Posted September 27, 2009 If you use file() you can get all the data of a file into an array (each line has it's own element). Then using array_pop() you can remove the last element from that array. Finally if you want the contents of the file in a string rather than an array use implode(). $file = file('somefile.txt'); array_pop($file); $content = implode("\n", $file); Link to comment https://forums.phpfreaks.com/topic/175739-remove-last-line-in-a-file-phpstore-all-lines-except-the-last-one/#findComment-926119 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.