欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

浏览文件神器!且可以计算所有文件代码的行数!

程序员文章站 2024-01-18 16:00:58
...

这个类可以列出文件的详细内容,统计一个目录下总代码行,你可以根据你的需要修改被统计的文件类型。 还可以限制统计类型 var$ext=array(php,phtml,php3,inc,js,html,htm,css,doc); 代码珠玑:http://www.codepearl.com/files/198.html PHP 源码与演示: 源码

这个类可以列出文件的详细内容,统计一个目录下总代码行,你可以根据你的需要修改被统计的文件类型。

还可以限制统计类型
var $ext = array("php","phtml","php3","inc","js","html","htm","css","doc");
代码珠玑:http://www.codepearl.com/files/198.html PHP

源码与演示:源码出处 演示出处

浏览文件神器!且可以计算所有文件代码的行数!浏览文件神器!且可以计算所有文件代码的行数!
 
//http://www.codepearl.com
header("Content-type: text/html; charset=UTF-8");
include("class.lineCount.php"); 

?>


	Source Code Count

	$lineCount = new lineCount;


	// If no directory is given, it will use the directory of the script

		$lineCount->dir="../linecount";


	// Use this method to output the summary and list of files to the page
	// You can customize the HTML from within the class

		$lineCount->summary(1);



	// Use this method to get the totals as an associative array:

		$totals = $lineCount->summary(0);

		# echo $totals["folders"] / $totals["files"] / $totals["lines"]

?>



//http://www.codepearl.com
header("Content-type: text/html; charset=UTF-8");

	class lineCount {

		// Don't modify these

			var $x   = 0;
			var $cnt = array();



		// Files to include in the count

			var $ext = array("php","phtml","php3","inc","js","html","htm","css","doc");


		// If no directory is set, the directory of the script will be used

			var $dir = "";




		function countLines($dir) {
            
			if (is_dir($dir)) {
                $dir=iconv("gb2312","GBK",$dir);
				if ($handle = opendir($dir)) {

					// Loop through files

					while (false !== ($file = readdir($handle))) { 
						if ($file != "." && $file != "..") { 

							$filePath = $dir."/".$file;

							if (is_dir($filePath)) {

								// Item is another folder, call the function again

								$this->countLines($filePath);

							} else {

								// Item is a file, get some info about it

								$fileName = explode("/",$filePath);
								$fileDir  = $fileName[(count($fileName)-2)];
								$fileName = $fileName[(count($fileName)-1)];
								$fileExt  = explode(".",$fileName);
								$fileExt  = $fileExt[(count($fileExt)-1)];

								if (in_array($fileExt,$this->ext)) {

									// Open the file, get line count

									$fp           = fopen($filePath, "r");
									$buffer       = rawurlencode(fread($fp, filesize($filePath)));
									$buffer       = explode("%0A", $buffer);
									$numLines     = count($buffer);
									fclose($fp);

									// Add the information to our count array

									$this->cnt[$this->x]['dir']   = $dir;
									$this->cnt[$this->x]['file']  = $fileName;
									$this->cnt[$this->x]['count'] = $numLines;
									$this->x++;

								}
							}

						} 
					}

					closedir($handle);

				} else {

					return false;

				}

			} else {

				return false;

			}

		}



		function summary($output=1) {

			// $output
			//    1 to generate a summary and file list
			//    0 to get an associative array with the totals

			if (!(is_dir($this->dir))) { 
				// No directory given, use document root
				$this->dir = $_SERVER['DOCUMENT_ROOT'];
			}

			$this->countLines($this->dir);


			$listOutput = "";
			$totalLines = 0;
			$usedDir    = array();

			for ($i=0;$icnt);$i++) {

				$totalLines += $this->cnt[$i]['count'];

				if (!(in_array($this->cnt[$i]['dir'],$usedDir))) {
					if ($output==1) {
						$listOutput .= "	\n";
						$listOutput .= "		".iconv("GB2312","UTF-8",$this->cnt[$i]['dir'])."\n";
						$listOutput .= "	\n";
					}
					$usedDir[] = $this->cnt[$i]['dir'];
				}
				if ($output==1) {
					$listOutput .= "	\n";
					$listOutput .= "		".iconv("GB2312","UTF-8",$this->cnt[$i]['file'])."\n";
					$listOutput .= "		".number_format($this->cnt[$i]['count'])."\n";
					$listOutput .= "	\n";
				}
			}

			$totalFiles   = number_format(count($this->cnt));
			$totalLines   = number_format($totalLines);
			$totalFolders = number_format(count($usedDir));



			if ($output==1) {

				print "
\n"; print "".$this->dir."

\n\n"; print "\n"; print " \n"; print " \n"; print " \n"; print "
简述: ".$totalFolders."文件夹, ".$totalFiles."文件, ".$totalLines." 行代码
\n"; print "\n"; print " \n"; print " \n"; print " \n"; print " \n"; print $listOutput; print "
文件名/目录代码行数
\n"; print "\n"; print " \n"; print " \n"; print " \n"; print "
简述: ".$totalFolders."文件夹, ".$totalFiles."文件, ".$totalLines."行代码
\n"; print "
\n"; } else { return array("files"=>$totalFiles,"lines"=>$totalLines,"folders"=>$totalFolders); } } } ?>