By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,664 Members | 1,669 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,664 IT Pros & Developers. It's quick & easy.

Number of lines

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

Thanks,

Fred

Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"Fred Atkinson" <fa*******@mishmash.com> wrote in message
news:mv********************************@4ax.com...
Is there a function that returns the number of lines in a text
file? If so, what is it?

Thanks,

Fred


http://ie2.php.net/manual/en/function.file.php

Returns an array, count() it.
Jul 17 '05 #2

P: n/a
Fred Atkinson wrote:
Is there a function that returns the number of lines in a text
file? If so, what is it?


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($contents = file_get_contents($file), "\n");
// may leave this out ^^^^^^^^^^^^

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

http://www.php.net/count
http://www.php.net/substr_count

--
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. |
Jul 17 '05 #3

P: n/a
Pedro Graca wrote:
Fred Atkinson wrote:
Is there a function that returns the number of lines in a text
file? If so, what is it?

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($contents = file_get_contents($file), "\n");
// may leave this out ^^^^^^^^^^^^


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

--
Justin Koivisto - sp**@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.
Jul 17 '05 #4

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


If it's a Unix/Linux server:

$lines=trim(`wc --lines < /path/to/file`);
--
--
-- Álvaro G. Vicario - Burgos, Spain
--
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.