Home > php教程 > PHP源码 > 统计代码行数和过滤注释

统计代码行数和过滤注释

PHP中文网
Release: 2016-05-25 17:08:18
Original
1007 people have browsed it

#!/usr/local/bin/php -f
<?php

/// 获取命令行参数
$fileName = $argv[1];
if ( ! is_dir( $fileName ) && ! file_exists( $fileName ) ) {
    
    echo "Usage: ".$argv[0]." [path|file]\n";
    exit;
}


$disableFile = "DMO|test_|FileZip|jpgraph|smarty|tourdata|demo_|Frame|socket.php|IDNA2.php|timezone.php|config.db.php|config.php";
$disableFile .= "|main1.php|obj.php|interface.php|hmac.php|php-excel.class.php|cfg.class.php|global.php|gconfig.php|main2.php|sinfo.php";
$disableFile .= "|main.php|index.php|mod_tpl.php|redis.lua|^_";


$disableDir = "cache$|templates$|templates_c$|adodb$|smarty$|mailer$|biz$|test$";
$ext = "\.php$|\.c$|\.h$|\.go$|\.lua$";

if ( is_dir( $fileName ) ) {
    getDir( $fileName );
} else {
    echoCode( $fileName );
}


function echoCode( $fileName ) {

    $fileLines = file( $fileName );
    $lineCount = 0;
    foreach( $fileLines AS $line ) {
        $line = str_replace( "\t", "    ", $line );
        $tr = trim( $line );
        if ( preg_match( "/\\*|^\*|^ {1,}\*|\/\*|\*\/|^ {1,}\/\/|^\/\//", $line ) || $tr === "" ) {
            continue;
        }
        $lineCount++;
        $line = str_replace( "\r", "", $line );
        $line = str_replace( "\n", "", $line );
        
        echo "".$line."\n";
    }
    echo "\n";
}

function getDir( $path ) {
    global $disableFile, $disableDir, $ext;
    $dDirs = dir( $path );
    while ( false !== ( $fileDirs = $dDirs->read() ) ) { 

        $sCodeFile = $path . "/".$fileDirs;
   
        if ( $fileDirs == "." || $fileDirs == ".."  || preg_match("/".$disableDir."/", $fileDirs ) ) {
            continue;
        }
        
        if ( ! is_dir( $sCodeFile ) && ! file_exists( $sCodeFile ) ) {
            continue;
        }
        if ( preg_match( "/".$disableFile."/", $fileDirs ) || 
            ( ! is_dir( $sCodeFile ) && ! preg_match( "/".$ext."/", $fileDirs ) ) ) {
            continue;
        }

        if ( is_dir( $sCodeFile ) ) {
            getDir( $sCodeFile );
            continue;
        } else {
        
            //echo $sCodeFile."\n";
            echo iconv( "UTF-8", "GBK", "文件名: ").$fileDirs."\n";
            //echo "文件名: ".$fileDirs."\n";
            echoCode( $sCodeFile );
        }
    }
}

?>
Copy after login

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
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template