Counting paragraphs in a file : fgets « File Directory « PHP






Counting paragraphs in a file

 
<?php
$paragraphs = 0;

if ($fh = fopen('great-american-novel.txt','r')) {
  while (! feof($fh)) {
    $s = fgets($fh);
    if (("\n" == $s) || ("\r\n" == $s)) {
      $paragraphs++;
    }
  }
}
print $paragraphs;
?>
  
  








Related examples in the same category

1.Checking for an error from fopen(), fgets(), or fclose()
2.Counting records in a file
3.Creating and Using a File Class
4.Reading a compressed file
5.Reading a file a line at a time
6.fgets() function returns a string read from a file.
7.fgets.php
8.Processing a list of books
9.Processing each word in a file