教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 统计代码行数与过滤注释的php代码

统计代码行数与过滤注释的php代码

发布时间:2015-09-13   编辑:jiaochengji.com
如何统计php代码的行数,以及如何过滤掉其中的注释呢?那么本文可以为大家提供一种思路与实现方法。此代码可用于整理代码,以及日常文档的注释提取等。

统计php代码的行数,过滤掉其中的注释。

复制代码 代码示例:

<?php
/// 获取命令行参数
/// by http://www.jiaochengji.com
$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 );
        }
    }
}
?>

您可能感兴趣的文章:
php 统计字数(支持中英文)的实现代码
php 防注入的一段代码(过滤参数)
php正则过滤html特殊字符
统计代码行数与过滤注释的php代码
防止sql注入与跨站攻击的代码分享(初级实用型)
php特殊字符转义函数
php代码中的空白与注释怎么去除
php特殊字符转义详解
php压缩html(清除换行符,清除制表符,去掉注释标记)
php表单提交特殊字符过滤方法

关键词: php注释   
[关闭]
~ ~