从父目录中的所有PHP文件中搜索特定的字符串 [英] Searching for a specific string from all PHP files in the parent directory

查看:88
本文介绍了从父目录中的所有PHP文件中搜索特定的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我试图找出一种搜索父目录中所有* .php文件的方法, p> / content / themes / default /


我不想搜索子目录中的所有文件。我想搜索嵌入在PHP注释语法中的字符串,例如:

  / *名称:default * / 

如果找到变量,则获取文件名和/或路径。我曾试着用google搜索这个,并想到自定义的方式来做到这一点,这是我迄今为止所尝试的:

  public function build_active_theme(){
$ dir = CONTENT_DIR。 '主题/'。 $ 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);
$ count ++;
if(strpos($ file_contents,'Main')){
$ array_pos = $ count;
$ main_file = $ theme_files [$ array_pos];

echo $ main_file;





所以你可以看到我将所有找到的文件添加到一个数组中,然后获取每个文件的内容,并搜索它以查找变量Main,如果找到该变量,则获取当前自动递增的数字,并从数组中获取路径,但它总是告诉我错误的文件,它没有接近'Main'。



我相信像Wordpress这样的CMS使用类似的插件开发功能,它会搜索所有文件中正确的插件细节(这是我想要做的,但主题)。

谢谢,
Kieron

解决方案

像David在他的评论中说的,数组在PHP中是零索引的。在用作$ theme_files的索引之前,$ count正在递增($ count ++)。将$ count ++移到循环结尾,并在索引查找后递增。

  public function build_active_theme() {
$ dir = CONTENT_DIR。 '主题/'。 $ 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 ++;
}

}


I am trying to figure out a way of searching through all of the *.php files inside the parent directory, parent directory example:

/content/themes/default/

I am not wanting to search through all of the files in the sub-directories. I am wanting to search for a string embedded in the PHP comment syntax, such as:

/* Name: default */

If the variable is found, then get the file name and/or path. I have tried googling this, and thinking of custom ways to do it, this is what I have attempted so far:

public function build_active_theme() {
    $dir = CONTENT_DIR . '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);
        $count++;
        if(strpos($file_contents, 'Main')) {
            $array_pos = $count;
            $main_file = $theme_files[$array_pos];

            echo $main_file;
        }
    }
}

So as you can see I added all the found files into an array, then got the content of each file, and search through it looking for the variable 'Main', if the variable was found, get the current auto-incremented number, and get the path from the array, however it was always telling me the wrong file, which had nothing close to 'Main'.

I believe CMS's such as Wordpress use a similar feature for plugin development, where it searches through all the files for the correct plugin details (which is what I want to make, but for themes).

Thanks, Kieron

解决方案

Like David said in his comment arrays are zero indexed in php. $count is being incremented ($count++) before being used as the index for $theme_files. Move $count++ to the end of the loop, And it will be incremented after the index look up.

public function build_active_theme() {
$dir = CONTENT_DIR . '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++;
}

}

这篇关于从父目录中的所有PHP文件中搜索特定的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆