![]() ![]() You may choose whatever memory size you want for the interpreter. If($handle) $linecount = substr_count($handle, PHP_EOL) However, if one have no problem with the memory available on the host system, like the OP, and proper memory limit in PHP is set (larger than the file length), substr_count can search the entire content of file with much performance: (2<<24)+(filesize($file)) ) // 32 MB for PHP + File size Directories 3 Files 10 Size Lines of Code (LOC) 1882 Comment Lines of Code (CLOC) 255 (13.55) Non-Comment Lines of Code (NCLOC) 1627 (86.45) Logical Lines of Code (LLOC) 377 (20.03) Classes 351 (93.10) Average Class Length 35 Minimum Class Length 0 Maximum Class Length 172 Average Method Length 2 Minimum Method Length 1 Maximum M. PHP program to count the number of lines in a text file : mytextfile mytextfile.txt alllines file(mytextfile) numberoflines count(. The code is taking into consideration the use of least memory (2 MB chunks).īenchmark with a 85 MB file and 8M+ lines, execution time is: Perhaps you could use: length count (file (filename)) Which would work everywhere. For example, the value of LINE depends on the line that it's used on in your script. $linecount += substr_count($chunk,"\n") // also with \n, \r, or \r\n PHP Manual Language Reference Constants Change language: Submit a Pull Request Report a Bug Magic constants There are nine magical constants that change depending on where they are used. $linecount += substr_count($chunk,PHP_EOL) If it doesn't work properly (at macintosh files for example), take a look here. file () reads the file into an array, split on newlines, and count () counts the length of an array. This is using substr_count and is much faster than fgets: $file="largefile.txt" Perhaps you could use: length count (file (filename)) Which would work everywhere.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |