23.6.1. ProblemYou want to count the number of lines, paragraphs, or records in a file. 23.6.2. SolutionTo count lines, use fgets( ), as in Example 23-18. Because it reads a line at a time, you can count the number of times it's called before reaching the end of a file. Counting lines in a file
To count paragraphs, increment the counter only when you read a blank line, as in Example 23-19. Counting paragraphs in a file
To count records, increment the counter only when the line read contains just the record separator and whitespace. In Example 23-20, the record separator is stored in $record_separator. Counting records in a file
23.6.3. DiscussionIn Example 23-18, $lines is incremented only if fgets( ) returns a true value. As fgets( ) moves through the file, it returns each line it retrieves. When it reaches the last line, it returns false, so $lines isn't incremented incorrectly. Because EOF has been reached on the file, feof( ) returns true, and the while loop ends. Example 23-19 works fine on simple text but may produce unexpected results when presented with a long string of blank lines or a file without two consecutive line breaks. These problems can be remedied with functions based on preg_split( ). If the file is small and can be read into memory, use the pc_split_paragraphs( ) function shown in Example 23-21. This function returns an array containing each paragraph in the file. pc_split_paragraphs( )
In Example 23-21, the contents of the file are broken on two or more consecutive newlines and returned in the $matches array. The default record-separation regular expression, \r?\n, matches both Windows and Unix line breaks. If the file is too big to read into memory at once, use the pc_split_paragraphs_largefile( ) function shown in Example 23-22, which reads the file in 16 KB chunks. pc_split_paragraphs_largefile( )
This function uses the same regular expression as pc_split_paragraphs( ) to split the file into paragraphs. When it finds a paragraph end in a chunk read from the file, it saves the rest of the text in the chunk in $unmatched_text and prepends it to the next chunk read. This includes the unmatched text as the beginning of the next paragraph in the file. The record-counting function in Example 23-20 lets fgets( ) figure out how long each line is. If you can supply a reasonable upper bound on line length, stream_get_line( ) provides a more concise way to count records. This function reads a line until it reaches a certain number of bytes or it sees a particular delimiter. Supply it with the record separator as the delimiter, as in Example 23-23. Counting records in a file with stream_get_line( )
Example 23-23 assumes that each record is no more that 64 KB (65,536 bytes) long. Each call to stream_get_line( ) returns one record, not including the record separator. When stream_get_line( ) has advanced past the last record separator, it reaches the end of the file, so $done is set to TRue to stop counting records. 23.6.4. See AlsoDocumentation on fgets( ) at http://www.php.net/fgets, on feof( ) at http://www.php.net/feof, on preg_split( ) at http://www.php.net/preg-split, and on stream_get_line( ) at http://www.php.net/stream_get_line. |