Search for phrase/word in text files with php
Solution 1
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.
Solution 2
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.
Solution 3
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();
?>
Solution 4
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]);
usertest
Updated on June 12, 2022Comments
-
usertest almost 2 years
How would I scan a directory for a specific line of text and list all matching files with php?
Thanks.