views:

56

answers:

5

How would I scan a directory for a specific line of text and list all matching files with php?

Thanks.

A: 

Well, first you might want to get a list of the files of interest with glob(if you want multiple extensions, simply merge the resulting arrays or use this). Then loop through the result, open the files with file_get_contents and check for your string with strpos.

voodoo555
multiple extensions can be done easier with: `glob('*.{ext1,ext2,ext3}', GLOB_BRACE)`
Tim Cooper
+1  A: 

An alternative is to read the php files, put the content into arrays and use something like preg_grep.

If the number of files is potentially very big, you might want to use the UNIX grep command together with a php exec.

I would personally go for the second solution.

Roberto Aloi
+3  A: 

I actually wrote a function for this a few days ago...

Here's the base function that scans each file...

foreach (glob("<directory>/*.txt") as $search) {
    $contents = file_get_contents($search);
    if (!strpos($contents, "text")) continue;
    $matches[] = $search;
}

Not the most advanced way of doing it, my function is much longer but it also uses all functions from my various other classes, this is basically what it does though.

animuson
A: 

Here is a trivial example of how this could be accomplished strictly in php...

  1. Get a list of all the files/directories within a directory.

  2. Check that each file/dir name is a file

  3. Get the contents of a file

  4. Use a string search function to look for matches of the string we're looking for. If a match exists, print the file name

Meep

<?php
$path = 'c:\\some\\cool\\directory';
$findThisString = 'Cool Cheese';

$dir = dir($path);

// Get next file/dir name in directory
while (false !== ($file = $dir->read()))
{   
    if ($file != '.' && $file != '..')
    {
        // Is this entry a file or directory?
        if (is_file($path . '/' . $file))
        {
            // Its a file, yay! Lets get the file's contents
            $data = file_get_contents($path . '/' . $file);

            // Is the str in the data (case-insensitive search)
            if (stripos($data, $findThisString) !== false)
            {
                // sw00t! we have a match
            echo 'match found in ' . $file . "<br>\n";
            }
        }
    }
}

$dir->close();

?>
John Himmelman
A: 

I won't put my recommended answer here, because 5 people have already posted great answers on how to solve this, but will recommend an alternative.

Have you considered using the PHP implementation of the Lucene Search Engine? The most notable one is from the Zend Framework. The best thing is that you do not have to use the framework to use the Lucene library (just include the library base file - remembering to add the Zend Libraries directory to the include path).

I have not used it myself, and have heard very mixed reviews about it. The only thing I could think of is that it may be far too complex for a small script or project.

A great detailed overview of the Lucene Library is at the Zend Framework reference guide.

mynameiszanders