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

查找第一行或第二行包含超过N个字符的所有php文件,php,regex,grep,find,Php,Regex,Grep,Find,我将如何继续编写一个程序来查找第一行或第二行包含超过N个字符的所有php文件?我想显示文件名和行的内容 我可以使用以下文件逐个文件: <html><head><title>Find String</title></head><body> <?php find_files('.'); function find_files($seed) { if(! is_dir($see

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

我可以使用以下文件逐个文件:

    <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>
查找字符串

我将使用注释从备份中恢复,并识别和消除该漏洞。但要回答这个问题:

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";
    }
}

昨天有一个类似的问题:同样的问题。

这是现实世界的要求吗?:-)@达贡,你什么意思?我已经在我的php文件中注入了代码,通常内容是在第一行或第二行注入的…@HommerSmith你不知道还有什么被注入或修改,也不知道在哪里。唯一明智的做法是删除所有内容并从干净的备份中恢复。之后,修补软件以关闭攻击所通过的漏洞。如果这是问题所在,这不是解决问题的方法,请从备份恢复。您不能确定使用此方法将获得所有注入的代码。我知道我需要从备份还原。我责备服务器上那些说“没有备份”的家伙。谢谢大家的建议。那么linux脚本会以更性感的方式实现吗?我还不知道,但我很确定“查找”可以做类似的事情。是的,检查OP的其他问题的链接。