开发者

fgets() PHP read last line?

开发者 https://www.devze.com 2023-02-06 03:22 出处:网络
Using the fgets() function (or any other way) in PHP is there a way to read the LAST line of a file then work backwards?

Using the fgets() function (or any other way) in PHP is there a way to read the LAST line of a file then work backwards? What I am doing: (as per request) I am appending to a .t开发者_如何学编程xt file lines from an input (don't worry about that) and of course append adds to the END of a file so I need to display the contents of a file with one line being one entry to be displayed

So if the contents of the file are:

Hello, World!

Foo likes Bar!

then it needs to display as

1. Foo likes Bar!

2. Hello, World!


As far as I know PHP does not know the gets() function. But..

If you use 'file()' and inverse the array you can work it through from bottom to top. That should do the trick for you!


If it is a really big file, it might be better to use the following...

exec("tail -1 input_file > output_file") ;
$string = file_get_contents("output_file") ;
unlink("output_file") ;

This is based on UNIX/Linux system commands, but I'm sure Windows has a command similar to tail.

0

精彩评论

暂无评论...
验证码 换一张
取 消