Connecting Tech Pros Worldwide Help | Site Map

Read only first 10 lines of a file into an array?

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 04:19 AM
deko
Guest
 
Posts: n/a
Default Read only first 10 lines of a file into an array?

It's nice to be able to generate an html table from a PHP array. I know how to
do this, but the array in question is built from a file. The file in question
can be very long, and I only want the first 10 lines. So, I'd like to reduce
the overhead it takes to read the file into the array by limiting the number of
lines read in - rather than have the entire file read in and then limiting the
output to the html table.

$visits= file("visdata.txt");
//reverse the array so table is ordered by date decending
$visits = array_reverse ($visits);
//show only the last 10 visitors
$number_of_visits = count($visits);
if ($number_of_visits > 10)
{
$number_of_visits = 10;
}
echo
"<table border='0' cellspacing='1' width='500'>";
....
....
....
....

There must be a way to limit how many lines from the file are read into the
array -- any suggestions?

Thanks in advance.





  #2  
Old July 17th, 2005, 04:19 AM
David Precious
Guest
 
Posts: n/a
Default Re: Read only first 10 lines of a file into an array?

deko wrote:
[color=blue]
> $visits= file("visdata.txt");[/color]
<snip>[color=blue]
> There must be a way to limit how many lines from the file are read into
> the array -- any suggestions?
>
> Thanks in advance.[/color]

Instead of the visits = file("visdata.txt"); line, try something like:

$fh = @fopen('visdata.txt','r');
if ($fh)
{
for ($i=0;$i<10;$i++)
{
$visits[] = fread($fh,4096);
}
} else {
// handle error opening file here
}
// $visits is an array containing the first 10 lines of the file.


That ought to do the trick.

Cheers

Dave P


--
David Precious
http://www.preshweb.co.uk/

  #3  
Old July 17th, 2005, 04:19 AM
deko
Guest
 
Posts: n/a
Default Re: Read only first 10 lines of a file into an array?

Hi and thanks for the reply.

Yes, using fopen like that would work, but I liked reading the file into an
array because I didn't have to open the file - no flock() to deal with, more
efficient, etc...

"David Precious" <pinkmeat@preshweb.co.uk> wrote in message
news:1079833150.73255.0@despina.uk.clara.net...[color=blue]
> deko wrote:
>[color=green]
> > $visits= file("visdata.txt");[/color]
> <snip>[color=green]
> > There must be a way to limit how many lines from the file are read into
> > the array -- any suggestions?
> >
> > Thanks in advance.[/color]
>
> Instead of the visits = file("visdata.txt"); line, try something like:
>
> $fh = @fopen('visdata.txt','r');
> if ($fh)
> {
> for ($i=0;$i<10;$i++)
> {
> $visits[] = fread($fh,4096);
> }
> } else {
> // handle error opening file here
> }
> // $visits is an array containing the first 10 lines of the file.
>
>
> That ought to do the trick.
>
> Cheers
>
> Dave P
>
>
> --
> David Precious
> http://www.preshweb.co.uk/
>[/color]


  #4  
Old July 17th, 2005, 04:20 AM
Chung Leong
Guest
 
Posts: n/a
Default Re: Read only first 10 lines of a file into an array?

Uzytkownik "David Precious" <pinkmeat@preshweb.co.uk> napisal w wiadomosci
news:1079833150.73255.0@despina.uk.clara.net...[color=blue]
> Instead of the visits = file("visdata.txt"); line, try something like:
>
> $fh = @fopen('visdata.txt','r');
> if ($fh)
> {
> for ($i=0;$i<10;$i++)
> {
> $visits[] = fread($fh,4096);
> }
> } else {
> // handle error opening file here
> }
> // $visits is an array containing the first 10 lines of the file.[/color]

I think you want fgets() instead of fread().

Wrapping it in a function:

function dog($path, $num = 10) {
$fh = @fopen($path, 'r');
if ($fh)
{
for ($i=0;$i<$num;$i++)
{
$array[] = fgets($fh,1024);
}
} else {
// handle error opening file here
}
return $array;
}


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.