mark.four
mark.four

Reputation: 9

How to count the number of non blank lines in php

I have been using this code to count blank lines in a text file

$filePath = ".somefile.txt";
$num = count(file($filePath));

which works fine but includes blank lines in the count, can I use an option such as SKIP BLANK LINES To count only lines that contain text?

I tried the above code but the result gives the total number of lines but includes blank lines

Upvotes: 0

Views: 467

Answers (1)

user18692108
user18692108

Reputation:

Yes, you can use flag FILE_SKIP_EMPTY_LINES.

Example:

File test.txt:

HELLO
HELLO1

HELLO2
HELLO3

Code:

$file  = file("test.txt", FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
$lines = count($file);
echo $lines; // 4

Upvotes: 3

Related Questions