reverse reading a text file

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

    reverse reading a text file

    how can i read for example 10 last lines from a text file beginning
    from last line? do you have some idea?

    in a different manner... how can i set file position indicator one line
    upper in text file (line ends with "\n") ?

    --
    pozdr.
    xEM~
  • Jason

    #2
    Re: reverse reading a text file

    > how can i read for example 10 last lines from a text file beginning[color=blue]
    > from last line? do you have some idea?[/color]

    One way:

    $file = array_reverse( file( 'mine.txt' ) );

    foreach ( $file as $line ){
    echo "$line\n";
    }

    As for the second question, I'm not sure what you mean.


    Comment

    • Jason

      #3
      Re: reverse reading a text file

      Sorry, I'm on crack:

      $file = array_reverse( file( 'mine.txt' ) );
      $limit = 10;

      for ($i = 0; $i <= $limit; $i++ ){
      echo $file[$i] . "\n";
      }



      Comment

      • Jason

        #4
        Re: reverse reading a text file

        > > how can i read for example 10 last lines from a text file beginning[color=blue][color=green]
        > > from last line? do you have some idea?[/color][/color]

        Oops, you wanted 10 lines:

        $file = array_reverse( file( 'mine.txt' ) );
        $limit = 10;

        for ($i = $limit; $i >= 0; $i-- ){
        echo $file[$i] . "\n";
        }



        Comment

        • Jason

          #5
          Re: reverse reading a text file

          Maybe I should test these before I reply?
          THIS ONE SPITS OUT TEN LINES :)

          $file = array_reverse( file( 'mine.txt' ) );
          $limit = 10;

          for ($i = 0; $i < $limit; $i++ ){
          echo $file[$i] . "\n";
          }




          Comment

          • Andy Hassall

            #6
            Re: reverse reading a text file

            On 11 Oct 2003 10:02:59 GMT, xEM <xem_@boxmail.b iz> wrote:
            [color=blue]
            >how can i read for example 10 last lines from a text file beginning
            >from last line? do you have some idea?[/color]

            There's more than one way to do it, but how about:

            <pre>
            <?php
            $file = 'test.txt';

            $fp = fopen($file, 'r');

            if ($fp) {
            $lines = array();

            while (($line = fgets($fp)) !== false) {
            $lines[] = $line;

            while (count($lines) > 10)
            array_shift($li nes);
            }

            foreach (array_reverse( $lines) as $line) {
            print $line;
            }

            fclose($fp);
            }
            ?>
            </pre>

            i.e. keep a running buffer of 10 lines (adding lines to the end, and shifting
            them off the start if it exceeds 10 lines).

            Then you said beginning from last line, so reverse the array before output.
            [color=blue]
            >in a different manner... how can i set file position indicator one line
            >upper in text file (line ends with "\n") ?[/color]

            Use fseek and fgetc working backwards until you reach the \n you want?




            --
            Andy Hassall (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
            Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

            Comment

            Working...