>  기사  >  백엔드 개발  >  통계 공유를 달성하는 PHP 코드

통계 공유를 달성하는 PHP 코드

小云云
小云云원래의
2018-03-14 09:52:291403검색

이 글은 주로 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;//输出总的代码量
?>

관련 추천:

php 코드 통계 도구

위 내용은 통계 공유를 달성하는 PHP 코드의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.