1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

how to start reading text file at specific line number

Discussion in 'PHP' started by alexts, Aug 14, 2007.

  1. #1
    Hi

    how can i start reading a file at specific line number.
    I want to start reading this file at line 20 for example

    Thanks for your help

    Here is my code

    $fh = fopen($myFile, 'r');

    while ((feof ($fh) === false) )
    {

    $theData = fgets($fh);

    echo $theData;

    }

    fclose($fp);
     
    alexts, Aug 14, 2007 IP
  2. bobmeetsworld

    bobmeetsworld Well-Known Member

    Messages:
    758
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    120
    #2
    depends on your editor
     
    bobmeetsworld, Aug 14, 2007 IP
  3. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Well, you can't just skip 20 lines forward. It's a sequential read of the file not random access.

    So all you can do is quickly skip the first 20 lines by ignoring them...

    
    $fh = fopen($myFile, 'r');
    
    while ((feof ($fh) === false) )
    {
      $i=1;
      while ((feof ($fh) === false) && $i<20 )
      {
        fgets($fh);
        $i++;
      }
    
      $theData = fgets($fh);
      echo $theData;
    
    }
    
    fclose($fh);
    
    PHP:
     
    ecentricNick, Aug 14, 2007 IP
  4. alexts

    alexts Well-Known Member

    Messages:
    1,126
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    125
    Digital Goods:
    1
    #4

    Thank a lot.I thought skipping too.
    I just thought there is any other way to do it.
     
    alexts, Aug 14, 2007 IP
  5. aras

    aras Active Member

    Messages:
    533
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    60
    #5
    
    
    $f = file_get_contents('file');
    $lines = explode("\r\n",$f);
    
      for ($k=19; $k<=count($lines); $k++) {
      // maybe?
      }
    
    
    PHP:
    Would be better for random reading, not always have to skip this way.
     
    aras, Aug 14, 2007 IP
  6. alexts

    alexts Well-Known Member

    Messages:
    1,126
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    125
    Digital Goods:
    1
    #6
    Thanks,

    I see there is no other way except skipping
     
    alexts, Aug 15, 2007 IP