duanlu6268
duanlu6268
2016-05-22 01:54

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

已采纳

A while ago I made a post (Searching for a specific string from all PHP files in the parent directory) that was about me finding the position of the file path in an array, only if the file had a specific keyword.

However, that was in my old website, which I have now lost. So I am currently recreating it. However, for some reason this function does not work.

  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++;
}

}

This function causes the following error:

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

I have narrowed the problem down the something wrong with the $count variable. Whenever I try and echo the $count value once the script has found the correct file, nothing is shown.

But after spending nearly an hour on such a simple problem, it is obviously starting to frustrate me, so I am now seeking help.

(Note: I directly copied the function directly from the old post into my code, and made the appropriate changes to variables to 'work' in the new site, so it's is pretty much exactly the same as the solution that fixed my previous problem - which funnily enough was also caused by the $count variable).

Thanks, Kieron

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • doucaishi0077 doucaishi0077 5年前

    You can use the foreach $key instead of a separate count variable, try the code below:

    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;
        }
    }
    
    点赞 评论 复制链接分享
  • douzhi4056 douzhi4056 5年前

    You are setting $count = null; , try to ++ it before the $array_pos = $count;

    点赞 评论 复制链接分享

相关推荐