duanlu6268 2016-05-21 17:54
浏览 60
已采纳

从父目录中的所有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 2016-05-21 18:00
    关注

    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;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部