PHP code to achieve statistical sharing

小云云
Release: 2023-03-21 10:58:01
Original
1402 people have browsed it

This article mainly shares with you the php code to implement statistical functions. I hope it can help you learn how to implement statistics in php code.

<?php
$filename = "D:/code/";//php代码所在目录
$counts = 0;
function codeCount($filename)
{
    global $counts;
    $total = 0; // 总行数
    $space = 0; // 空行数
    $notes = 0; // 注释
    $handle = fopen($filename, "r");
    $isNotes = false;
    while (! feof($handle)) {
        $line = fgets($handle);
        $total ++;
        if ($isNotes) {
            $notes ++;
            if (preg_match("/.*(\*\/)/", $line)) { // 多行*/注释结束
                $isNotes = false;
            }
            continue;
        }
        if (preg_match("/^[\s]*$/", $line)) { // 空行
            $space ++;
        } elseif (preg_match("/^[\s]*\/\//", $line)) { // 两杠注释
            $notes ++;
        } elseif (preg_match("/^[\s]*(\/\*).*(\*\/)[\s]*$/", $line)) { // 单行注释
            $notes ++;
        } elseif (preg_match("/^[\s]*(\/\*).*/", $line)) { // 多行/*注释开始
            $notes ++;
            $isNotes = true;
        }
    }
    echo "total:" . $total . "\r\n";
    echo "space:" . $space . "\r\n";
    echo "notes:" . $notes . "\r\n";
    echo "<br>";
    $counts += ($total - $space - $notes);
}
if (is_file($filename)) {
    codeCount($filename);
} else 
    if (is_dir($filename)) {
        if ($dh = opendir($filename)) {
            while (($file = readdir($dh)) != false) {
                // 文件名的全路径 包含文件名
                $filePath = $filename . $file;
                // 获取文件修改时间
                if (is_file($filePath)) {
                    codeCount($filePath);
                }
            }
            closedir($dh);
        }
    }
echo "<br>" . $counts;//输出总的代码量
?>
Copy after login

Related recommendations:

php code statistics tool

The above is the detailed content of PHP code to achieve statistical sharing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!