Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP]licz linijki kodu
Forum PHP.pl > Forum > Przedszkole
kavillock
otóż w google znalazłem funkcję tj.
  1. function countlines() {
  2. $d = dir(".");
  3. while($entry = $d->read()) {
  4. if ($entry != "." && $entry != "..") {
  5. if (!is_dir($dirName."/".$entry)) {
  6. $ext = substr($entry,strpos($entry,"."),strlen($entry));
  7. if ($ext==".php" || $ext==".css" || $ext==".js" || $ext==".htm" || $ext==".html") {
  8. $count += count(file($entry));
  9. }
  10. }
  11. }
  12. }
  13. return $count;
  14. $d->close();
  15. }


jednak ma ona jedną wadę, nie liczy tego co się znajduję w podfolderach, wiecie jak mogę ją o to urozmaicić questionmark.gif
korro
  1. <?php
  2.  
  3. /**
  4. * Counts the lines of code in this folder and all sub folders
  5. * You may not sell this script our remove these header comments
  6. * @author Hamid Alipour, <a href="http://www.hamidof.com/" target="_blank">http://www.hamidof.com/</a>, Codehead @ <a href="http://talk.code-head.com/" target="_blank">http://talk.code-head.com/</a> Codehead Webmaster Forums
  7. **/
  8.  
  9. define('SHOW_DETAILS', true);
  10.  
  11. class Folder {
  12.  
  13. var $name;
  14. var $path;
  15. var $folders;
  16. var $files;
  17. var $exclude_extensions;
  18. var $exclude_files;
  19. var $exclude_folders;
  20.  
  21.  
  22. function Folder($path) {
  23. $this -> path = $path;
  24. $this -> name = array_pop( array_filter( explode(DIRECTORY_SEPARATOR, $path) ) );
  25. $this -> folders = array();
  26. $this -> files = array();
  27. $this -> exclude_extensions = array('gif', 'jpg', 'jpeg', 'png', 'tft', 'bmp', 'rest-of-the-file-extensions-to-exclude');
  28. $this -> exclude_files = array('count_lines.php', 'rest-of-the-files-to-exclude');
  29. $this -> exclude_folders = array('_private', '_vti_bin', '_vti_cnf', '_vti_log', '_vti_pvt', '_vti_txt', 'rest-of-the-folders-to-exclude');
  30. }
  31.  
  32. function count_lines() {
  33. if( defined('SHOW_DETAILS') ) echo "/Folder: {$this -> path}...\n";
  34. $total_lines = 0;
  35. $this -> get_contents();
  36. foreach($this -> files as $file) {
  37. if( in_array($file -> ext, $this -> exclude_extensions) || in_array($file -> name, $this -> exclude_files) ) {
  38. if( defined('SHOW_DETAILS') ) echo "#---Skipping File: {$file -> name};\n";
  39. continue;
  40. }
  41. $total_lines += $file -> get_num_lines();
  42. }
  43. foreach($this -> folders as $folder) {
  44. if( in_array($folder -> name, $this -> exclude_folders) ) {
  45. if( defined('SHOW_DETAILS') ) echo "#Skipping Folder: {$folder -> name};\n";
  46. continue;
  47. }
  48. $total_lines += $folder -> count_lines();
  49. }
  50. if( defined('SHOW_DETAILS') ) echo "\Total lines in {$this -> name}: $total_lines;\n\n";
  51. return $total_lines;
  52. }
  53.  
  54. function get_contents() {
  55. $contents = $this -> _get_contents();
  56. foreach($contents as $key => $value) {
  57. if( $value['type'] == 'Folder' ) {
  58. $this -> folders[] = new Folder($value['item']);
  59. } else {
  60. $this -> files[] = new File ($value['item']);
  61. }
  62. }
  63. }
  64.  
  65. function _get_contents() {
  66. $folder = $this -> path;
  67. if( !is_dir($folder) ) {
  68. return array();
  69. }
  70. $return_array = array();
  71. $count = 0;
  72. if( $dh = opendir($folder) ) {
  73. while( ($file = readdir($dh)) !== false ) {
  74. if( $file == '.' || $file == '..' ) continue;
  75. $return_array[$count]['item'] = $folder .$file .(is_dir($folder .$file) ? DIRECTORY_SEPARATOR : '');
  76. $return_array[$count]['type'] = is_dir($folder .$file) ? 'Folder' : 'File';
  77. $count++;
  78. }
  79. closedir($dh);
  80. }
  81. return $return_array;
  82. }
  83.  
  84. } // Class
  85.  
  86. class File {
  87.  
  88. var $name;
  89. var $path;
  90. var $ext;
  91.  
  92.  
  93. function File($path) {
  94. $this -> path = $path;
  95. $this -> name = basename($path);
  96. $this -> ext = array_pop( explode('.', $this -> name) );
  97. }
  98.  
  99. function get_num_lines() {
  100. $count_lines = count(file($this -> path));
  101. if( defined('SHOW_DETAILS') ) echo "|---File: {$this -> name}, lines: $count_lines;\n";
  102. return $count_lines;
  103. }
  104.  
  105. } // Class
  106.  
  107. echo '<pre>';
  108. $path_to_here = dirname(__FILE__) .DIRECTORY_SEPARATOR;
  109. $folder = new Folder($path_to_here);
  110. echo 'Total lines of code: ' .$folder -> count_lines() ."\n\n";
  111. echo '</pre>';
  112.  
  113. ?>
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.