如何在多个 .php 文件中搜索?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7198479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:14:58  来源:igfitidea点击:

How to search in multiple .php files?

phpsearch

提问by Wordpressor

I have around 100 .php files in one directory and I'm looking for one small function, what's the fastest way to search trough all contents of these files?

我在一个目录中有大约 100 个 .php 文件,我正在寻找一个小功能,搜索这些文件的所有内容的最快方法是什么?

[edit]

[编辑]

I'm using Windows 7 Ultimate/NuSphere PhpED.

我正在使用 Windows 7 Ultimate/NuSphere PhpED。

回答by AVProgrammer

Try this:

尝试这个:

<?php
function getFilesWith($folder, $searchFor, $extension = 'php') {

    if($folder) {
        $foundArray = array();
        // GRAB ALL FILENAMES WITH SUPPLIED EXTENSION
        foreach(glob($folder . sprintf("*.%s", $extension)) as $file) {
            $contents = file_get_contents($file);

            if(strpos($contents, $searchFor) !== false) {
                $foundArray[] = $file;
            }
        }

        if(count($foundArray)) {
            return $foundArray;
        } else {
            return false;
        }
    } else {
        return false;
    }
}

$matched_files = getFilesWith('path/to/folder', 'Looking for this');
?>

回答by Byron Whitlock

Use your php editors "find in files" function.

使用您的 php 编辑器“在文件中查找”功能。

Invaluable.

无价。

editPHPNuSpheretotally supports this. you need to learn some google fu brother.

编辑PHPNuSphere完全支持这一点。你需要学习一些google fu兄弟。

If your editor doesn't have this, you need to switch ASAP. https://stackoverflow.com/search?q=php+editor

如果你的编辑器没有这个,你需要尽快切换。 https://stackoverflow.com/search?q=php+editor

Find in files in Visual Studio

Find in files in Notepad++

在 Visual Studio 中的文件中查找

在 Notepad++ 中的文件中查找

回答by Ed Heal

Install cgywin - then you can use grep!

安装 cgywin - 然后你就可以使用 grep 了!

回答by Victor

function search_in_dir( $dir, $str )
{
    $files = glob( "{$dir}/*.php" );
    foreach( $files as $k => $file )
    {
        $source = file_get_contents( $file );
        if( strpos( $source, $str ) === false )
        {
            unset( $files[$k] );
        }
    }
    return array_filter( $files );
}
$files = search_in_dir( 'dir/files', 'my string' );

回答by seyedrezabazyar

I use the following code

我使用以下代码

<?php
$folder = 'folder';
echo '<p><b>We Find</b> in <span style="color:red">'. __DIR__ .'</span></p> ';

foreach (glob("$folder/*.php") as $filename) {

$file = file_get_contents($filename);

if( strpos( $file, 'text' ) === false ){
    echo '<span style="color:red">not found: </span>';
}else{
    echo '<span style="color:blue">found: </span>';
}

echo $filename.'<br>';

if (file_put_contents($filename, preg_replace("/text/", "new text", $file))) {

} else {
    echo "not found :(";
}
}

Create a folder in the path to your code
Put your files in that folder
The files containing your text are displayed in blue. Then the texts will be replaced

在您的代码路径中创建一个文件夹
将您的文件放在该文件夹中
包含您的文本的文件显示为蓝色。然后文本将被替换

回答by eon

For Window I would install cygwinand use find or grep, but failing that

对于 Window,我会安装cygwin并使用 find 或 grep,但失败了

  • Install total commander, use Alt + F7 to search recursively. There's also a replace in multiple files option - http://www.ghisler.comYou will wonder how you ever navigated your system without it

  • using Notepad++you can open all files and do a normal text search just check the box 'search in all open files'

  • 安装总指挥官,使用 Alt + F7 递归搜索。还有一个替换多个文件选项 - http://www.ghisler.com你会想知道你是如何在没有它的情况下导航你的系统的

  • 使用Notepad++您可以打开所有文件并进行普通文本搜索,只需选中“在所有打开的文件中搜索”框