#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Location
    Australia
    Posts
    4
    Rep Power
    0
    How can I count how many lines are in a textfile?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Location
    Pune, Maharashtra, India
    Posts
    86
    Rep Power
    15
    <?
    function countlines($list,$ctr)
    {
    $lines = file($list);

    while(list($key,$val) = each($lines))
    {
    $ctr++;
    }
    $line = implode("", $lines);
    $fileh = fopen($list, "w");
    fwrite($fileh, $line);
    fclose($fileh);
    return($ctr);
    }

    $st=countlines("test",0);
    echo $st;
    ?>
    Make a simple function like this which counts the number of lines of a file (made by me)Thanks. This takes he file name and 0 as the parameters and returns the number of lines
    Best of luck then.

    ------------------
    Nikunj
    MYSQL/PHP/XML
    ** Expertise comes with experiece ** Nikunj
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Location
    Australia
    Posts
    4
    Rep Power
    0
    thanks works great, how can i get it to only run if the file exsists? because when the file is not there it produces an error
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Location
    Pune, Maharashtra, India
    Posts
    86
    Rep Power
    15
    Try checking for the existence of file just before is called this can be done by:
    <?
    if(@is_file($filename))
    {
    $st=countlines("test",0);
    echo $st;
    }
    else
    {
    echo ("file not found");
    }
    ?>



    ------------------
    Nikunj
    MYSQL/PHP/XML
    ** Expertise comes with experiece ** Nikunj
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2000
    Posts
    669
    Rep Power
    15
    I think this can be done a whole lot simpler:

    if ($file = file($fileyouwanttocount)) {
    $count = count($file);
    }
    else {
    print "No file there";
    }

    $count now holds the number of lines
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2000
    Posts
    669
    Rep Power
    15
    I didn't even explain myself, sorry. The file() function returns an array from the parameter you pass with it, holding each line of the file as a new element of the array.
    so $file = file("http://yahoo.com");

    would return an array named $file holding each line of the file as a new eleement so you could just assign count($file) to a variable to return the number of elements in the file thus you would have the number of lines.
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Location
    Australia
    Posts
    4
    Rep Power
    0
    sweet, another problem, I am counting multiple files on the same page and unset($var); doesn't seem to fix the problem with needing the same $var over and over.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2000
    Posts
    669
    Rep Power
    15
    I am assuming the variable you are using to hold your file() array is the one you want to use over so after each go try just setting it to nothing like:
    $file = "";

Similar Threads

  1. How to count max appearances?
    By Stew_McGruff in forum MySQL Help
    Replies: 8
    Last Post: December 13th, 2003, 06:27 PM
  2. count number of lines in paragraph
    By km1234 in forum HTML Programming
    Replies: 3
    Last Post: December 12th, 2003, 02:14 AM
  3. words count in a sentence
    By alebaba in forum C Programming
    Replies: 5
    Last Post: November 22nd, 2003, 09:54 AM
  4. Reading as many lines from an FFDB as exist?
    By Duminas in forum Perl Programming
    Replies: 2
    Last Post: October 3rd, 2003, 11:49 PM
  5. printing required lines to a file.
    By prince in forum Perl Programming
    Replies: 5
    Last Post: July 19th, 2003, 10:29 AM

IMN logo majestic logo threadwatch logo seochat tools logo