duanlu6268 2016-05-22 01: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-22 02: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条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么