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! Quote 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); Quote 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
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.