Count lines in all php files

How can I count all of the lines in all of the files in the current directory and child directories?

It uses option to find for matching extensions, and parameter to get file list from standard input Question: How to make a little function that given a directory it returns the number of lines (counts ) of files between ? I want to count recursively including javascript, html, css and php files to get the total number of combined lines of code.

How can I count all of the lines in all of the files in the current directory and child directories?

I’m trying to figure out how many lines of code have been written for an app. Code is in the current directory and child directories. I’m using ubuntu.

If you just want the total lines you can use the following command:

find . -name \*.c -o -name \*.h -exec cat <> \; | wc -l

find . -type f -name \*.c -exec wc -l <> \; > /tmp/c_counts find . -type f -name \*.h -exec wc -l <> \; > /tmp/h_counts 

This will produce the wc output for each file with a particular extension, one extension per /tmp file. You could run these results through a simple awk script to get the grand total, if that’s what you need.

Count lines of code in directory Code Example, // For Windows dir -Recurse *.* | Get-Content | Measure-Object -Line ^ Extension of the files to consider.

How to count all the lines of code in a directory recursively using or operator to include several file types

I am reading this answer: How to count all the lines of code in a directory recursively?

But it only works for php files . I want to count recursively including javascript , html, css and php files to get the total number of combined lines of code.

I tried this find . -name ‘*.php’ |’*.js’ | xargs wc -l

Note: I am using OS X El Capitan.

find \( -name '*.php' -o -name '*.js' -o -name '*.html' \) -exec wc -l <> + 
  • \( \) grouping so that all results are given to wc command
  • add as many -o -name as required within \( \)
  • use iname instead of name to ignore case of filenames
  • by default, find works on current directory
  • https://unix.stackexchange.com/questions/12902/how-to-run-find-exec
  • https://unix.stackexchange.com/questions/24954/when-is-xargs-needed
  • https://unix.stackexchange.com/questions/131766/why-does-my-shell- script-choke -on-whitespace-or-other-special-characters/131767#131767
Читайте также:  Base64 decode to image python

find . -name ‘*.php’ | xargs wc -l

find . -name ‘*.php’ -o -name ‘*.js’ | xargs wc -l

To not include certain path:

find . -name «*.php» -not -path «./tests*» | xargs wc -l

Here is a one liner. It uses find regex option to find for matching extensions, and wc —files0-from=- parameter to get file list from standard input

find -regex '.*\(php\|js\|css\|html?\)' -printf '%p\0' |wc -l --files0-from=- 

Count lines of code in directory using Python, import os def countlines (start, lines=0, header=true, begin_start=none): if header: print (‘ <:>10> | <:>10> | | 11>| 20>’.format (», », »)) for thing in os.listdir (start): thing = os.path.join (start, thing) if os.path.isfile (thing): if thing.endswith (‘.py’): with open (thing, ‘r’) as f: newlines = f.readlines …

Counting all lines of code in .php files given a dir

How to make a little function that given a directory it returns the number of lines (counts \r\n ) of .php files between ?

echo countsLineOfCode('D:/dir/code/'); // returns 323; 
function countLinesOfCode($path) < $lines = 0; $items = glob(rtrim($path, '/') . '/*'); foreach($items as $item) < if (is_file($item) AND pathinfo($item, PATHINFO_EXTENSION) == 'php') < $fileContents = file_get_contents($item); preg_match_all('/<\?(?:php)?(.*?)($|\?>)/s', $fileContents, $matches); foreach($matches[1] as $match) < $lines += substr_count($match, PHP_EOL); >> else if (is_dir($item)) < $lines += countLinesOfCode($item); continue; >> return $lines; > var_dump(countLinesOfCode(dirname(__FILE__))); // int(31) (works for me) 

Keep in mine this is counting newlines, not end of line character ; . For example, the line below will be considered one line.

var_dump($files); echo 'something'; exit; 

It also counts lines without any PHP code, e.g. the below code will be 4 lines.

Let me know if it (a) shouldn’t match empty lines, (b) should match semi colons instead (will need to ensure they are not appearing within a string) and/or (c) it should match the opening and closing tag (will be easy as changing $matches[1] in the foreach to $matches[0] ).

PHPLOC

A tool for quickly measuring the size and analyzing the structure of a PHP project.

recursive version of alex function

function countLinesOfCode($path) < $lines = 0; $files = glob(rtrim($path, '/') . '/*'); foreach($files as $file) < if (is_dir($file))< if ($file=='.' || $file=='..') continue; $lines+=countLinesOfCode($file); >else if (substr($file,-4)!='.php') continue; echo 'Counting on ' . $file .'
'; $fileContents = file_get_contents($file); preg_match_all('/<\?(?:php)?(.*?)(?:$|\?>)/s', $fileContents, $matches); foreach($matches[1] as $match) < $lines += substr_count($match, PHP_EOL); >> return $lines; >

I made it work based on this Answer and the documentation for recursivedirectoryiterator .

$path = realpath( '/path/to/directory' ); $lines = $files = 0; $objects = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $path ), RecursiveIteratorIterator::SELF_FIRST ); foreach( $objects as $name => $fileinfo ) < if ( !$fileinfo->isFile() ) continue; if( false === strpos( $name,'.php' ) ) continue; $files++; $read = $fileinfo->openFile(); $read->setFlags( SplFileObject::READ_AHEAD ); $lines += iterator_count( $read ) - 1; // -1 gives the same number as "wc -l" > printf( "Found %d lines in %d files.", $lines, $files ); 

Language agnostic — Count total number of lines in a, Using the command: wc -l + `find . -name \* -print` You can get the total number of lines of all files inside a folder. But imagine you have some folders (for example libraries), which you don’t want to count their lines because you didn’t write them. So, how would you count the lines in a project excluding certain …

Источник

Count Lines in File in PHP

To count the number of lines in a file in PHP, you can use one of the following approaches:

Use the file() function

This function reads the entire file into an array, with each line of the file as an element in the array. Then, use the count() function to count the number of elements in the array.

Here is an example of how to count lines in file in PHP using the file() method.

Note that this approach counts empty lines that exist within the file.

This code assumes that you have file.txt in current folder. You can provide relative path or absolute path based on the preference.

Use the file_get_contents() function

Using the FILE_IGNORE_NEW_LINES flag

Here, we can count lines in a file using the file_get_contents() function with the FILE_IGNORE_NEW_LINES flag. This function reads the entire file into a string, ignoring newline characters. Then, use the substr_count() function to count the number of newline characters in the string.

The FILE_IGNORE_NEW_LINES ignores the newlines that are empty, and simply focuses on lines with characters.

Using the FILE_SKIP_EMPTY_LINES flag

Here we make use of the file_get_contents() function alongside the FILE_SKIP_EMPTY_LINES flag to count lines in file.

This function reads the entire file into a string, skipping empty lines. Then, use the substr_count() function to count the number of newline characters in the string.

Here is an example script on how to use the function.

The FILE_SKIP_EMPTY_LINES ignores the newlines that are empty, and simply focuses on lines with characters.

Use the fopen() and fgets() functions

Another approach we can take is count lines in file using a loop. This approach makes use of the fopen() and fgets() function to reads the file line by line, incrementing a counter variable for each line.

Note that this approach counts empty lines that exist within the file.

fopen() method with r is used to open file in read mode.

fgets() method is used to get line from file pointer in PHP.

That’s all about how to count lines in file in PHP.

Was this post helpful?

Share this

Author

Remove Last Character from String in PHP

Table of ContentsUse the substr() functionUse the rtrim() functionUse the mb_substr() functionUse the preg_replace() functionUse the str_split() and array_pop() function This article explains different ways to remove the last character from a string in PHP Use the substr() function To remove last character from String in PHP, use of the substr() function and pass a […]

Check if Variable is Array in PHP

Table of ContentsUsing the is_array() functionUsing the gettype() functionUsing the typesetting syntax Using the is_array() function To check if variable is array in PHP, use is_array() function. The is_array() function is a built-in function in PHP that takes a variable as an argument and returns a Boolean value indicating whether the variable is an array. […]

Split String by Comma in PHP

Table of ContentsUse the explode() functionUse the str_getcsv() functionUse the preg_split() functionUse the mb_split() function This article explains how to split a string by comma in PHP using different approaches. Use the explode() function To split string by comma, we can make use of the explode function. This function splits the string by the specified […]

Format Number to 2 Decimal Places in PHP

Table of ContentsUsing the round() functionUsing the number_format() functionUse sprintf() functionUse the floatval and number_format function Using the round() function To format a number to 2 decimal places in PHP, you can use the round() function and pass the number and 2 as arguments to the round() function. Here is an example of how to […]

Check if Array is Empty in PHP

Table of ContentsUse empty() functionUse count() functionUse array_key_exists() functionUse array_filter() functionUse array_reduce() function Use empty() function To check if an array is empty in PHP, you can use the empty() function. This function accepts an array as its argument, and it returns true if the array is empty and false if the array is not […]

Replace Space with Underscore in PHP

Table of ContentsUsing str_replace() MethodUsing preg_replace() Method Using str_replace() Method Use the str_replace() function to replace with underscore in PHP, for example: str_replace(» » , «_», $str). str_replaces() returns new String with old character space( ) replaced by underscore(_) [crayon-64bc7fa0bb87e445505017/] [crayon-64bc7fa0bb891967642452/] We used str_replace() to replace space with underscore in PHP. str_replace() replaces all instances […]

Источник

How to count the number of lines in a text file using PHP

Hello, I have posted several posts on working with a text file in PHP. Some of those were really helpful to the learners. Like
How to read a particular line from a text file in PHP
Fetching Text Data From a Text File Using PHP
Save HTML Form Data in a (.txt) Text File in PHP
But here in this post, I am going to show you How to count the number of lines in a text file using PHP.

Count the number of lines in a text file using PHP

To count the number of lines in a text file in PHP we need to use these below PHP function:

To get a better Idea you may read this post, this post is very similar to this one How to read a particular line from a text file in PHP You just need to extend a single line.

So let’s create a text file first.

Hello I am from line no 1 Hi I am from line no 2 hello again I am from line no 3 Hey I am from line no 4

Now you need to count the number of lines in this text file. Here we have 4 lines.

Save this file with any filename. Here I am going to save it as mytextfile.txt

PHP program to count the number of lines in a text file :

This above program will give you the number of lines in a text file.

Special note: The file() function actually returns an array. So here $all_lines variable holds the whole text file as an array. count() function will count the number of elements in the array.

Источник

Оцените статью