2 Questions

Discussion in 'PHP' started by t7584, Apr 18, 2006.

  1. #1
    1. Is It Possible To Count Number Of Lines In A File Without Creating An Array
    2. How To Move File Pointer To A Specified Line
     
    t7584, Apr 18, 2006 IP
  2. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,334
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #2
    For the number of lines:
    $lines = substr_count (file_get_contents ('filename.txt'), "\n");
    PHP:
    In order to set the file pointer to a specific line, you are going to need to calculate where that line starts (in bytes). You may need to loop the strpos() function (with the optional offset parameter) once for each line to ultimately figure out that value.
     
    digitalpoint, Apr 18, 2006 IP
  3. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Sounds like homework questions from back when I was studying C :eek:
     
    exam, Apr 18, 2006 IP
  4. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,334
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #4
    I just remembered how you can get the offsets of each carriage return without looping...

    preg_match_all ("#\\n#", file_get_contents('filename.txt'), $matches, PREG_OFFSET_CAPTURE);
    print_r ($matches);
    PHP:
     
    digitalpoint, Apr 18, 2006 IP