This was posted on 07/17/2011 and was filed in PHP, Code Snippets | (No Comments Yet)
function get_line_count($folder, $pos) {
global $ignoreList, $extList;
if ( !is_dir($folder) ) return false;
$dir = opendir($folder);
while ($thisFile = readdir($dir))
{
if ( in_array($thisFile, $ignoreList) ) continue;
$thisLoc = $folder.'/'.$thisFile;
if ( is_dir($thisLoc) ) {
$thisCount += get_line_count($thisLoc, $pos+1);
echo '<br/>';
for ( $x = 1; $x <= $pos; $x++ ) echo '....';
echo $thisFile.'/';
}
else {
$thisExt = array_pop(explode('.',$thisFile));
if ( !in_array($thisExt, $extList) ) continue;
echo '<br/>';
for ( $x = 1; $x <= $pos; $x++ ) echo '....';
echo $thisFile.'/';
$thisContents = file_get_contents($thisLoc);
$thisContents = str_replace(array("nn","r"), "n",$thisContents);
$thisContents = str_replace("nn", "n",$thisContents);
$thisLines = count(explode("n",$thisContents));
$thisCount += $thisLines;
echo ' <b>('.$thisLines.')</b>';
}
}
return $thisCount;
}
//File or folder names to ignore
$ignoreList = array('.htaccess','cgi-bin','.','..','error_log');
//Only files with these extensions will be checked
$extList = array('php','css','js','html');
//Set your starting dir
$startDir = $_SERVER['DOCUMENT_ROOT'];
$lines = get_line_count($startDir, 0);
echo '<p>'.$lines.' lines of code found.</p>';
global $ignoreList, $extList;
if ( !is_dir($folder) ) return false;
$dir = opendir($folder);
while ($thisFile = readdir($dir))
{
if ( in_array($thisFile, $ignoreList) ) continue;
$thisLoc = $folder.'/'.$thisFile;
if ( is_dir($thisLoc) ) {
$thisCount += get_line_count($thisLoc, $pos+1);
echo '<br/>';
for ( $x = 1; $x <= $pos; $x++ ) echo '....';
echo $thisFile.'/';
}
else {
$thisExt = array_pop(explode('.',$thisFile));
if ( !in_array($thisExt, $extList) ) continue;
echo '<br/>';
for ( $x = 1; $x <= $pos; $x++ ) echo '....';
echo $thisFile.'/';
$thisContents = file_get_contents($thisLoc);
$thisContents = str_replace(array("nn","r"), "n",$thisContents);
$thisContents = str_replace("nn", "n",$thisContents);
$thisLines = count(explode("n",$thisContents));
$thisCount += $thisLines;
echo ' <b>('.$thisLines.')</b>';
}
}
return $thisCount;
}
//File or folder names to ignore
$ignoreList = array('.htaccess','cgi-bin','.','..','error_log');
//Only files with these extensions will be checked
$extList = array('php','css','js','html');
//Set your starting dir
$startDir = $_SERVER['DOCUMENT_ROOT'];
$lines = get_line_count($startDir, 0);
echo '<p>'.$lines.' lines of code found.</p>';
There are probably ten billion ways to do this, BUT I was looking for a way to quickly see how many lines I had coded for a project. This will use a recursive function to loop through a directory, scanning for any php/xhtml/css/js files and counting how many lines they might have inside.
Have fun!
Have I Helped You? Share Some Love!
Comments
» No Comments have been made on this. How about you start us off?
Comment On This
Browse By Category
- All Posts
- Useful Websites
- Money Savers
- Tips And Tricks
- Social Media
- Guides and Information
- PHP
- How-Tos
Latest Posts
- » PHP Unserializer
- » How To: Quickly Add a New PHP Extension Via SSH
- » Easily Open A TCP Port On Your Stock cPanel Server
- » Free Code: Find The Total Line Count Of A Folder Using PHP
- » Free Code: ReCaptcha Form Validation With PHP
- » View All
Advertising
© Max Morgan Design 2009 - 2012 | Page Generated In 0.18781 Seconds Using 42 MYSQL Queries


