Skip to content

Instantly share code, notes, and snippets.

@deekayen
Created February 28, 2013 17:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deekayen/5058566 to your computer and use it in GitHub Desktop.
Save deekayen/5058566 to your computer and use it in GitHub Desktop.
Count all the lines of programming vs comments in a file after all the files within a directory have been concatenated together.
<?php
// find . -type f -exec cat {} > ../results.txt \;
$data = file_get_contents('/Users/davidnorman/Sites/lowescat.txt');
$original = count(explode("\n", $data));
print "Original lines: ". $original;
print "<br />\n";
$data = preg_replace("/(\\/\\/.*\\n)/", "", $data); // remove single line comments, like this, from // to \\n
$data = preg_replace("/(\\/\\*.*\\*\\/)/", "", $data); // remove multi-line comments /* */
$data = preg_replace("/(<![^>]*>)/", "", $data); // remove multi-line comments <!-- -->
//$fp = fopen('/Users/davidnorman/Sites/lowescat_stripped.txt', 'w');
//fwrite($fp, $stripped);
//fclose($fp);
$filtered = count(explode("\n", $data));
print "Comment-filtered lines: ". $filtered;
print "<br />\n";
$math = (($original - $filtered) / $original) * 100;
print "Percent comments: $math%";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment