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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • deko

    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.t xt");
    //reverse the array so table is ordered by date decending
    $visits = array_reverse ($visits);
    //show only the last 10 visitors
    $number_of_visi ts = count($visits);
    if ($number_of_vis its > 10)
    {
    $number_of_visi ts = 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

    #2
    Re: Read only first 10 lines of a file into an array?

    deko wrote:
    [color=blue]
    > $visits= file("visdata.t xt");[/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.t xt"); 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


    Comment

    • deko

      #3
      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@presh web.co.uk> wrote in message
      news:1079833150 .73255.0@despin a.uk.clara.net. ..[color=blue]
      > deko wrote:
      >[color=green]
      > > $visits= file("visdata.t xt");[/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.t xt"); 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]


      Comment

      • Chung Leong

        #4
        Re: Read only first 10 lines of a file into an array?

        Uzytkownik "David Precious" <pinkmeat@presh web.co.uk> napisal w wiadomosci
        news:1079833150 .73255.0@despin a.uk.clara.net. ..[color=blue]
        > Instead of the visits = file("visdata.t xt"); 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;
        }


        Comment

        Working...