Re: including() only given amount of lines
| From: | Jason Wong | Date: | Sat, 10 May 2003 17:02:40 +0000 |
| Subject: | Re: including() only given amount of lines | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-147127@lists.php.net to get a copy of this message | ||
On Sunday 11 May 2003 00:45, iliketheworld@gmx.ch wrote:
> am 10.05.2003 15:44 Uhr schrieb Jason Wong unter php-general@gremlins.biz:
> > On Saturday 10 May 2003 19:53, iliketheworld@gmx.ch wrote:
> >> 1. i would like to include only a given amount (the first 50) lines of a
> >> text file. i think they should be included using fgets() or fread()
> >> instead of include() but i dont know how.
> >>
> >> 2. besides that i need to include the laste 50 lines of a text file --
> >> in reversed order!
> >
> > The easiest way would be to use file() and the array manipulation
> > functions.
>
> yes, but isn't a 500+ line file a bit heavy to read into an array? i
> thought this may cause massive slow down -- isn't it?
Assuming you have 500 lines each with 80 chars, that's around 40KB. Further
assume that in storing those lines into an array increases its storage
requirements ten-fold. That still only uses 400KB -- a piddling amount of
memory these days (unless you're running your webserver on 32MB).
For (1), if you wanted to do it using fgets() it's just a simple matter of
opening the file with fopen() then use a for-loop to call fgets() 50 times.
For (2), it's more complicated. You would probably have to use fread() then
count how many newline characters there are. Then loop N times and skip the
first N-50 lines and keep the rest.
For both (1) and (2) if you're using a un*x system and don't mind a
non-portable way of doing things then have a look at head and tail.
--
Jason Wong -> Gremlins Associates -> www.gremlins.biz
Open Source Software Systems Integrators
* Web Design & Hosting * Internet & Intranet Applications Development *
------------------------------------------
Search the list archives before you post
http://marc.theaimsgroup.com/?l=php-general
------------------------------------------
/*
Apathy Club meeting this Friday. If you want to come, you're not invited.
*/