Search content within file

1

I would like to create a php file and have it scan a directory and search for .txt files

Scan for example all .txt files in the / files / folder (and all subdirectories of it)

Each file that it sweeps I need it to try to find within the contents of the file, a keyword, such as "MARIA".

At the end he'd scour all the files, show something like this:

002023.txt - Maria found - Line 1 002023.txt - Maria found - Line 10 565454.txt - Maria found - Line 400 231332.txt - Maria found - Line 100

Can anyone help me?

Thank you

    
asked by anonymous 16.09.2016 / 00:29

1 answer

0

You can make foreach using glob () looking for all .txt in a certain folder, and within this foreach use the file_get_contents ( ) to vascular each .txt file to find the occurrences of words.

Something like this:

foreach (glob("SEU DIRETORIO/*.txt") as $arquivo) {
    $palavra = 'palavra procurada';
    header('Content-Type: text/plain');
    $conteudo = file_get_contents($arquivo);
    $parte = explode("\n", $conteudo);
    $base = preg_quote($palavra, '/');
    $base = "/^.*$base.*\$/m";
    if(preg_match_all($base, $parte[0], $achado)){
        echo "achou:\n";
        echo implode("\n", $achado[0]);
    }else{
        echo "não achou";
    }
}

I think that's enough to help you.

    
16.09.2016 / 14:44