需要有关如何在字符串中搜索多个文本文件的帮助

Right now it's only searching through one text file but I want it to search through a whole directory but I don't know how to go about that :/

<?php
$searchfor = "example";
$file = 'file.txt';
$contents = file_get_contents($file);
$pattern  = preg_quote($searchfor, '/');
$pattern  = "/^.*$pattern.*\$/m";
if (preg_match_all($pattern, $contents, $matches)) {
echo "<pre>Matches were found for $searchfor:
";
echo implode("
", $matches[0]);
?>

Instead of just searching through the one file "file.txt", I want it to search through a whole directory of files to find the string.

Thank you.

This should work:

<?php
$searchfor = "example";
$dir = ".";
$pattern  = preg_quote($searchfor, '/');
$pattern  = "/^.*$pattern.*\$/m";
$files = scandir($dir);
foreach($files as $file) {
    $contents = file_get_contents($file);
    if (preg_match_all($pattern, $contents, $matches)) {
        echo "<pre>Matches were found for $searchfor:
";
        echo implode("
", $matches[0]);
    }
}
?>