Get line count file php
OK, let's look into all of the options. Please note that some of these tools count all lines of code, not just PHP. When possible, I've passed filters to them to just count PHP files. You can require it globally or project-specific with Composer, or, my preferred method, just download the. CLOC is one of the longer-running and smartest programs for counting lines of code. It can differentiate languages and also separate empty lines and comment lines against real lines of code.
It can also pull from archives and git repositories, diff two versions of a codebase, pull from specific commits, ignore files and folders matching specific patterns, and it's installable via Brew, NPM, two Windows package managers, and all the major Linux package managers.
I found a plugin for PHPStorm called Statistic that gives you the total number of lines of code across your whole project and broken down by file type. I found this Gist , which harnesses the regex capabilities of Sublime Text search, and makes it easy to specify which file types and folders you want to include or exclude.
This is definitely one of the less precise measures, but it also doesn't require you to have anything else installed, and it gives you the ability to include and exclude specific patterns for files and folders. As you can see, we're excluding the two vendor directories, the git directory, and then you can also see an example of how to exclude and include specific file patterns. Thanks to Jake Bathman at Tighten for helping me get this command working correctly.
If you have Silver Searcher installed, you can try this:. Thanks to Daniel Coulbourne at Tighten for this one. I ran these tools all on the same project: Symposium , one of Tighten's open source projects, to see how they all compare. This post took way longer than I expected. Approach 2: Here we will load only one line of files at a time which is better than the1st approach. The PHP fgets function is used to return a line from an open file. The fclose function is used to close the file pointer.
This code will load the whole 1GB of data which can cause a memory overflow issue. Approach 3: We will load only a specific size of data into memory.
We will iterate on the loaded data to count the number of line breaks in it. Skip to content. Change Language. Related Articles.
Table of Contents. Improve Article. The first is to increase the availble memory allowed, which is probably not the best way to do things given that you state the file can get very large.
The other way is to use fgets to read the file line by line and increment a counter, which should not cause any memory issues at all as only the current line is in memory at any one time. John Reeve Article.
I use this method for purely counting how many lines in a file. What is the downside of doing this verses the other answers. I'm seeing many lines as opposed to my two line solution. I'm guessing there's a reason nobody does this. It does load the file into memory but doesn't need to cycle through an unknown number of lines. It may be unsuitable if the file is GB in size but for smaller files with short lines of data it works a treat for me.
I know it is not the best answer for all occasions but is something I have found quick and simple for my purposes where each line of the log is only a few hundred chars and total log file is not too large. How are we doing? Please help us improve Stack Overflow. Take our short survey.
Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Efficiently counting the number of lines of a text file. Asked 11 years, 11 months ago. Active 2 months ago. Viewed k times. Thanks all for any help. Improve this question. Abs Abs Add a comment. Active Oldest Votes. Improve this answer.
Dominic Rodger Dominic Rodger 93k 31 31 gold badges silver badges bronze badges. Why not improve a bit by limiting the line reading to 1? This depends on your setup. Do not forget: fgets does not read line by line. Won't this return 1 more than the number of lines? Show 6 more comments. Using a loop of fgets calls is fine solution and the most straightforward to write, however: even though internally the file is read using a buffer of bytes, your code still has to call that function for each line.
Community Bot 1 1 1 silver badge. Curious how faster?
0コメント