To do it over a directory, I'd use an iterator.

function countLines($path, $extensions = array('php')) {
    $it = new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator($path)
    );
    $files = array();
    foreach ($it as $file) {
        if ($file->isDir() || $file->isDot()) {
            continue;
        }
        $parts = explode('.', $file->getFilename());
        $extension = end($parts);
        if (in_array($extension, $extensions)) {
            $files[$file->getPathname()] = count(file($file->getPathname()));
        }
    }
    return $files;
}

这将返回一个数组,其中每个文件作为键和行数作为价值。然后,如果只想要总计,则只需执行 array_sum(countLines($ path)); ...

That will return an array with each file as the key and the number of lines as the value. Then, if you want only a total, just do array_sum(countLines($path));...

这篇关于PHP-如何计算应用程序中的代码行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 17:19