Connecting Tech Pros Worldwide Help | Site Map

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

deko
Guest
 
Posts: n/a
#1: Jul 17 '05
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.




David Precious
Guest
 
Posts: n/a
#2: Jul 17 '05

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/

deko
Guest
 
Posts: n/a
#3: Jul 17 '05

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]


Chung Leong
Guest
 
Posts: n/a
#4: Jul 17 '05

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;
}


Closed Thread