How would I scan a directory for a specific line of text and list all matching 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With