Number of lines

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

    Number of lines

    Is there a function that returns the number of lines in a text
    file? If so, what is it?

    Thanks,



    Fred

  • oeb -=- bleh bleh bleh

    #2
    Re: Number of lines


    "Fred Atkinson" <fatkinson@mish mash.com> wrote in message
    news:mvp0d01tk3 6m6ausqg698jav5 35o1p1329@4ax.c om...[color=blue]
    > Is there a function that returns the number of lines in a text
    > file? If so, what is it?
    >
    > Thanks,
    >
    >
    >
    > Fred
    >[/color]



    Returns an array, count() it.


    Comment

    • Pedro Graca

      #3
      Re: Number of lines

      Fred Atkinson wrote:[color=blue]
      > Is there a function that returns the number of lines in a text
      > file? If so, what is it?[/color]

      You have to read the file first.

      Either put the file contents in an array and count it:

      $lines = count($contents = file($file));
      // may leave ^^^^^^^^^^^^ this out!


      or put it in a string and count all the "\n"s

      $lines = substr_count($c ontents = file_get_conten ts($file), "\n");
      // may leave this out ^^^^^^^^^^^^




      http://www.php.net/file_get_contents (PHP >= 4.3.0)

      Counts all elements in an array or in a Countable object



      --
      USENET would be a better place if everybody read: | to email me: use |
      http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
      http://www.netmeister.org/news/learn2quote2.html | header, textonly |
      http://www.expita.com/nomime.html | no attachments. |

      Comment

      • Justin Koivisto

        #4
        Re: Number of lines

        Pedro Graca wrote:
        [color=blue]
        > Fred Atkinson wrote:
        >[color=green]
        >> Is there a function that returns the number of lines in a text
        >>file? If so, what is it?[/color]
        >
        >
        > You have to read the file first.
        >
        > Either put the file contents in an array and count it:
        >
        > $lines = count($contents = file($file));
        > // may leave ^^^^^^^^^^^^ this out!
        >
        >
        > or put it in a string and count all the "\n"s
        >
        > $lines = substr_count($c ontents = file_get_conten ts($file), "\n");
        > // may leave this out ^^^^^^^^^^^^[/color]

        Not if it's a Mac ASCII file... ;) Then it's the "\r"s that you need to
        count.

        --
        Justin Koivisto - spam@koivi.com
        PHP POSTERS: Please use comp.lang.php for PHP related questions,
        alt.php* groups are not recommended.

        Comment

        • Alvaro G Vicario

          #5
          Re: Number of lines

          *** Fred Atkinson wrote/escribió (Wed, 16 Jun 2004 15:34:18 GMT):[color=blue]
          > Is there a function that returns the number of lines in a text
          > file? If so, what is it?[/color]

          If it's a Unix/Linux server:

          $lines=trim(`wc --lines < /path/to/file`);


          --
          --
          -- Álvaro G. Vicario - Burgos, Spain
          --

          Comment

          Working...