Counting lines in large text files using file($path) can lead to memory exhaustion errors as the entire file is loaded into memory. To address this issue, a more efficient and memory-saving approach is required.
One method for efficiently counting lines is to use the fgets() function. By reading and processing lines one at a time, you can avoid loading the entire file into memory:
<code class="php">$file = "largefile.txt"; $linecount = 0; $handle = fopen($file, "r"); while (!feof($handle)) { $line = fgets($handle); $linecount++; } fclose($handle); echo $linecount;</code>
This code opens the file for reading, reads a single line at a time using fgets(), increments the line count, and terminates when the end of the file is reached.
Another approach, particularly useful for files with potentially very long lines, is to count end-of-line characters:
<code class="php">$file = "largefile.txt"; $linecount = 0; $handle = fopen($file, "r"); while (!feof($handle)) { $line = fgets($handle, 4096); $linecount += substr_count($line, PHP_EOL); } fclose($handle); echo $linecount;</code>
Here, substr_count() is used to count the number of line breaks in each chunk of data read.
Both these methods use less memory since they process lines incrementally rather than loading the entire file into memory.
The above is the detailed content of How to Efficiently Count Lines in Large Text Files (200 MB )?. For more information, please follow other related articles on the PHP Chinese website!