找到第一行或第二行超过N个字符的所有php文件

时间:2014-03-06 19:08:26

标签: php regex grep find

我如何继续编写一个程序来查找我的所有php文件,其中第一行或第二行包含多于N个字符?我想显示文件的名称和行的内容

我可以逐个文件地输入:

    <html><head><title>Find String</title></head><body>
    <?php
    find_files('.');
    function find_files($seed) {
        if(! is_dir($seed)) return false;
        $files = array();
        $dirs = array($seed);
        while(NULL !== ($dir = array_pop($dirs)))
        {
            if($dh = opendir($dir))
            {
                while( false !== ($file = readdir($dh)))
                {
                    if($file == '.' || $file == '..') continue;
                    $path = $dir . '/' . $file;
                    if(is_dir($path)) { $dirs[] = $path; }
                    else { if(preg_match('/^.*\.(php[\d]?)$/i', $path)) { check_files($path); }}
                }
                closedir($dh);
            }
        }
    }

    function check_files($this_file){

        if(!($content = file_get_contents($this_file)))
        { echo("<p>Could not check $this_file You should check the contents manually!</p>\n"); }
        else
        {
            // What to do here??
        }
        unset($content);
    }
    ?>
</body></html>

1 个答案:

答案 0 :(得分:0)

我会将评论从备份中恢复,并识别并消除漏洞。但要回答这个问题:

foreach(glob('path/*.php') as $file) {
    $lines = file($file);

    if(strlen($lines[0]) > $N) {
        echo "$file : line 1 : {$line[0]}\n";
    }
    elseif(strlen($lines[1]) > $N) {
        echo "$file : line 2 : {$line[1]}\n";
    }
}

昨天有一个类似的问题:Find php files with strings that have more than 50 characters有同样的问题。

相关问题