Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在多个.php文件中搜索?_Php_Search - Fatal编程技术网

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

如何在多个.php文件中搜索?,php,search,Php,Search,我在一个目录中有大约100个.php文件,我正在寻找一个小函数,通过这些文件的所有内容搜索最快的方法是什么 [编辑] 我正在使用Windows 7 Ultimate/NuSphere PhpED。使用php编辑器的“在文件中查找”功能 无价之宝 编辑完全支持这一点。你需要学习一些谷歌富兄弟 如果你的编辑器没有这个,你需要尽快切换。 试试这个: <?php function getFilesWith($folder, $searchFor, $extension = 'php') {

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

[编辑]

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

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

无价之宝

编辑完全支持这一点。你需要学习一些谷歌富兄弟

如果你的编辑器没有这个,你需要尽快切换。

试试这个:

<?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');
?>

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

  • 安装total commander,使用Alt+F7递归搜索。还有一个替换多个文件选项- 你会想知道没有它你是如何导航你的系统的

  • 使用可以打开所有文件并执行常规文本搜索,只需选中“搜索所有打开的文件”框


安装cgywin-然后您可以使用grep

我使用以下代码

<?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 :(";
}
}

如果您有Linux或Mac,您可以使用。如果你有windows,你可以使用。有些编辑器也有“在目录中查找文本”功能,但这取决于您使用的是什么。参考此项目:我使用的是NuSphere PhpED,我不确定它是否具有此功能。事实上,我从未梦想过在我的PHP编辑器中使用此功能,当时正在考虑完全不同的解决方案,例如与cmd相关的内容或PHP函数。我只需要点击两下就可以了;/非常感谢你!如果没有在文件中查找,我永远无法学习新的代码库;)祝你好运我的意思是,我为这一努力喝彩,但这太过分了@Birryrree共享主机访问权限有限这是确保其工作的唯一方法我同意…如果我们只有FTP访问权限,并且不想下载所有文件,则需要使用代码搜索…这将是我的答案+1.
<?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 :(";
}
}