本文主要和大家分享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代码实现统计分享的详细内容。