Search for phrase/word in text files with php
How would I scan a directory for a specific line of text and list all matc开发者_运维问答hing files with php?
Thanks.
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.
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.
Here is a trivial example of how this could be accomplished strictly in php...
Get a list of all the files/directories within a directory.
Check that each file/dir name is a file
Get the contents of a file
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();
?>
If files are big, it is overkill having to read each file into memory and then search its conents.
If you have read permissions over the directory, you could figure out the file where the needle is located by combining exec with egrep:
php > exec("egrep -rl 'string of what I want to find' full-or-relative-directory", $output);
php > print_r($output);
Array
(
[0] => full-or-relative-directory/foo/bar.xml
)
php > $contents = file_get_contents($output[0]);
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.
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.
$directory = "/var/www/application/store/"; //define the path
$files1 = scandir($directory); //scandir will scan the directory
$c = count($files1); //this will count all the files in the directory
print $c;
精彩评论