Home >Backend Development >PHP Tutorial >How Can You Efficiently Count Lines in Gigabytes of Text Data?
For text files exceeding 200 MB, counting lines using count(file($path)) can encounter memory limitations. Here's a more efficient solution:
<code class="php">$file = "largefile.txt"; $linecount = 0; $handle = fopen($file, "r"); while (!feof($handle)) { fgets($handle); $linecount++; } fclose($handle); echo $linecount;</code>
This approach loads a single line at a time into memory, avoiding the need to store the entire file in memory.
If your files may contain extremely long lines, you can use this alternative method to count line breaks instead:
<code class="php">$linecount = 0; $handle = fopen($file, "r"); while (!feof($handle)) { $line = fgets($handle, 4096); $linecount += substr_count($line, PHP_EOL); } fclose($handle);</code>
By chunking the file and counting line breaks, you can mitigate memory issues even with exceptionally long lines.
The above is the detailed content of How Can You Efficiently Count Lines in Gigabytes of Text Data?. For more information, please follow other related articles on the PHP Chinese website!