从父目录中的所有 PHP 文件中搜索特定字符串(更新)

Searching for a specific string from all PHP files in the parent directory (Updated)

不久前我做了一个post () 是关于我在一个数组中找到文件路径的位置,只有当文件有一个特定的关键字时。

但是,那是在我的旧网站上,现在我已经丢失了。所以我目前正在重新创建它。但是,由于某些原因,此功能不起作用。

  public function build_active_theme() {
$dir = CONPATH . '/themes/' . $this->get_active_theme() . '/';

$theme_files = array();
foreach(glob($dir . '*.php') as $file) {
    $theme_files[] = $file;
}

$count = null;
foreach($theme_files as $file) {
    $file_contents = file_get_contents($file);
    if(strpos($file_contents, 'Main')) {
        $array_pos = $count;
        $main_file = $theme_files[$array_pos];

        echo $main_file;
    }
    $count++;
}

}

此函数导致以下错误:

Notice: Undefined index: in /home/u841326920/public_html/includes/class.themes.php on line 30

我已将问题缩小到 $count 变量的问题。每当我尝试在脚本找到正确的文件后回显 $count 值时,什么也没有显示。

但是在这么简单的问题上花了将近一个小时之后,显然开始让我感到沮丧,所以我现在正在寻求帮助。

(注意:我直接将旧post中的函数直接复制到我的代码中,并在新站点中对'work'的变量进行了适当的更改,所以它非常准确与解决我之前问题的解决方案相同 - 有趣的是,这也是由 $count 变量引起的)。

谢谢, 基隆

您可以使用 foreach $key 而不是单独的计数变量,试试下面的代码:

foreach($theme_files as $key => $file) {
    $file_contents = file_get_contents($file);
    if(strpos($file_contents, 'Main') !== false) {
        $main_file = $theme_files[$key];

        echo $main_file;
    }
}

您正在设置 $计数=空; ,尝试在 $array_pos = $count;