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






Counting records in a file

 
<?php
$records = 0;
$record_separator = '--end--';

if ($fh = fopen('great-american-textfile-database.txt','r')) {
  while (! feof($fh)) {
    $s = rtrim(fgets($fh));
    if ($s == $record_separator) {
      $records++;
    }
  }
}
print $records;
?>
  
  








Related examples in the same category

1.Checking for an error from fopen(), fgets(), or fclose()
2.Counting paragraphs 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