Jump to content

TuxToaster

New Members
  • Posts

    2
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

TuxToaster's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Ok, one more fix, this one includes checks to see how much code is between <?php and ?> tags... again there's probably a better or more accurate way, but this at least gives a rough idea. Used it because the project I'm doing includes alot of HTML outside the tags and I don't have time yet to get it separated like I'd like to. It also totals everything up at the end. Here it is: [code] <?php // Code Counter $comment_lines = 0; $code_lines = 0; $other_lines = 0; $total_lines = 0; $in_comment = false; foreach (glob("*.php") as $filename) {     $thefile = file($filename);     foreach($thefile as $key => $val){         if (trim($val) != '') {             if (preg_match('/\<\?php/', trim($val))) {                 $in_php = true;             }             if ($in_php) {                 if ($in_comment) {                     if (trim($val) != "*/") {                         $comment_lines++;                     }else{                         $comment_lines++;                         $in_comment = false;                     }                 }else{                     if (substr(trim($val),0,2) == "//") {                         $comment_lines++;                     }else if (substr(trim($val),0,2) == "/*") {                         $comment_lines++;                         if (substr(trim($val), -2) != "*/") {                             $in_comment = true;                         }                     }else{                         $code_lines++;                     }                 }             }else{                 $other_lines++;             }             if (preg_match('/\?\>/', trim($val))) {                 $in_php = false;             }         }     } } echo "Lines of PHP code: $code_lines\n"; echo "Lines of other code: $other_lines\n"; echo "Lines of comments: $comment_lines\n"; $total_lines = $code_lines + $other_lines; echo "---------------------------------------------\n"; echo "Total Lines of code: $total_lines\n"; $total_lines += $comment_lines; echo "Total Lines(including comments): $total_lines\n"; ?> [/code]
  2. Here's my updated version, that checks for comment blocks... I'm sure there's probably a better way to do this, but none that I could think of at the moment. It's a quick hack anyways ;-) [code] <?php $comment_lines = 0; $code_lines = 0; $in_comment = false; foreach (glob("*.php") as $filename) {     $thefile = file($filename);     foreach($thefile as $key => $val){         if (trim($val) != '') {             if ($in_comment) {                 if (trim($val) != "*/") {                     $comment_lines++;                 }else{                     $comment_lines++;                     $in_comment = false;                 }             }else{                 if (substr(trim($val),0,2) == "//") {                     $comment_lines++;                 }else if (substr(trim($val),0,2) == "/*") {                     $comment_lines++;                     if (substr(trim($val), -2) != "*/") {                         $in_comment = true;                     }                 }else{                     $code_lines++;                 }             }         }     } } echo "Lines of code: $code_lines\n"; echo "Lines of comments: $comment_lines\n"; ?> [/code]
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.