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 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效