php代码统计工具

工具使用php编写,要求文件下下的文件为php文件(也就是以.php结尾的文件),可以统计一个文件夹下的php代码量,代码如下:

<?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;//输出总的代码量
?>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值