计算给定dir的.php文件中的所有代码行
如何制作一个小函数,在给定目录的情况下,返回计算给定dir的.php文件中的所有代码行,php,Php,如何制作一个小函数,在给定目录的情况下,返回之间.php文件的行数(计数\r\n) 用法: echo countsLineOfCode('D:/dir/code/'); // returns 323; 用于快速测量PHP项目的大小和分析其结构的工具 用于快速测量PHP项目的大小和分析其结构的工具 它还计算没有任何PHP代码的行数,例如,下面的代码将是4行 <?php $a = 3; 它还计算没有任何PHP代码的行数,例如,下面的代码将是4行 <?php $a
之间.php
文件的行数(计数\r\n
)
用法:
echo countsLineOfCode('D:/dir/code/');
// returns 323;
用于快速测量PHP项目的大小和分析其结构的工具
用于快速测量PHP项目的大小和分析其结构的工具
它还计算没有任何PHP代码的行数,例如,下面的代码将是4行
<?php
$a = 3;
它还计算没有任何PHP代码的行数,例如,下面的代码将是4行
<?php
$a = 3;
alex函数的递归版本
function countLinesOfCode($path) {
$lines = 0;
$files = glob(rtrim($path, '/') . '/*');
foreach($files as $file) {
if (is_dir($file)){
if ($file=='.' || $file=='..')
continue;
$lines+=countLinesOfCode($file);
} else if (substr($file,-4)!='.php')
continue;
echo 'Counting on ' . $file .'<br>';
$fileContents = file_get_contents($file);
preg_match_all('/<\?(?:php)?(.*?)(?:$|\?>)/s', $fileContents, $matches);
foreach($matches[1] as $match) {
$lines += substr_count($match, PHP_EOL);
}
}
return $lines;
}
函数countLinesOfCode($path){
$lines=0;
$files=glob(rtrim($path,'/')。/*');
foreach($files作为$file){
if(is_dir($file)){
如果($file='.| |$file=''.')
继续;
$lines+=countLinesOfCode($file);
}else if(substr($file,-4)!='.php')
继续;
回显“依赖于“.$file”。
;
$fileContents=file\u get\u contents($file);
preg_match_all('/)/s',$fileContents,$matches);
foreach($matches[1]作为$match){
$lines+=substr\u count($match,PHP\u EOL);
}
}
返回$line;
}
alex函数的递归版本
function countLinesOfCode($path) {
$lines = 0;
$files = glob(rtrim($path, '/') . '/*');
foreach($files as $file) {
if (is_dir($file)){
if ($file=='.' || $file=='..')
continue;
$lines+=countLinesOfCode($file);
} else if (substr($file,-4)!='.php')
continue;
echo 'Counting on ' . $file .'<br>';
$fileContents = file_get_contents($file);
preg_match_all('/<\?(?:php)?(.*?)(?:$|\?>)/s', $fileContents, $matches);
foreach($matches[1] as $match) {
$lines += substr_count($match, PHP_EOL);
}
}
return $lines;
}
函数countLinesOfCode($path){
$lines=0;
$files=glob(rtrim($path,'/')。/*');
foreach($files作为$file){
if(is_dir($file)){
如果($file='.| |$file=''.')
继续;
$lines+=countLinesOfCode($file);
}else if(substr($file,-4)!='.php')
继续;
回显“依赖于“.$file”。
;
$fileContents=file\u get\u contents($file);
preg_match_all('/)/s',$fileContents,$matches);
foreach($matches[1]作为$match){
$lines+=substr\u count($match,PHP\u EOL);
}
}
返回$line;
}
我根据和的文档完成了这项工作
我使它工作的基础和文件
请告诉我,你不会用这个来衡量你的程序员在做多少工作!请告诉我,你不会用这个来衡量你的程序员在做多少工作!不应该是这样的(.*)>
类似(.*)\?>
?@yes123是的,现在进行一些更改并测试:)@yes123已更新。好像在处理我输入的样本数据:)@alex:伙计,有个大问题。。。它在sub-dir下不会发生下弯。这是个大问题。我想我们可以更改glob以匹配路径中的所有内容,并且在foreach中,如果它是dir(is_dir()),我们应该循环进入该子dir。但是我没有这样的经验(至少目前+1)@alex:对于空代码行和;签名这两个都不是什么大问题。不应该是这样的(.*)>
类似(.*)\?>
?@yes123是的,现在进行一些更改并测试:)@yes123已更新。好像在处理我输入的样本数据:)@alex:伙计,有个大问题。。。它在sub-dir下不会发生下弯。这是个大问题。我想我们可以更改glob以匹配路径中的所有内容,并且在foreach中,如果它是dir(is_dir()),我们应该循环进入该子dir。但是我没有这样的经验(至少目前+1)@alex:对于空代码行和;签名这两个都不是大问题。我不认为glob()
曾经给过你
或。
。我不确定这就是我检查的原因:我不认为glob()
曾经给过你
或。
。我不确定这就是我检查的原因:虽然这个链接可以回答这个问题,最好在这里包括答案的基本部分,并提供链接供参考。如果链接页面发生更改,仅链接的答案可能会无效。虽然此链接可以回答问题,但最好在此处包含答案的基本部分,并提供链接以供参考。如果链接页面发生更改,则仅链接的答案可能无效。
$path = realpath( '/path/to/directory' );
$lines = $files = 0;
$objects = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator( $path ),
RecursiveIteratorIterator::SELF_FIRST
);
foreach( $objects as $name => $fileinfo )
{
if ( !$fileinfo->isFile() )
continue;
if( false === strpos( $name,'.php' ) )
continue;
$files++;
$read = $fileinfo->openFile();
$read->setFlags( SplFileObject::READ_AHEAD );
$lines += iterator_count( $read ) - 1; // -1 gives the same number as "wc -l"
}
printf( "Found %d lines in %d files.", $lines, $files );