Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.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_Json - Fatal编程技术网

返回一个数组的Php代码,该数组包含包含字符串的文件的文件名

返回一个数组的Php代码,该数组包含包含字符串的文件的文件名,php,json,Php,Json,我试图创建一个Php文件,它不接收任何内容,并检查文件夹中的每个文件,搜索其中的字符串。它回显包含字符串的文件名数组。有没有办法做到这一点,可能是低内存使用率 非常感谢。要实现类似的效果,我建议您阅读有关该类以及PHP的内容 下面是一个示例,说明如何读取给定目录($dir)的内容,并使用它搜索每个文件内容($contents)中出现的特定字符串: 这一条很容易在网上搜索 <?php $dir = '.'; if (substr($dir, -1) !== '/') { $dir

我试图创建一个Php文件,它不接收任何内容,并检查文件夹中的每个文件,搜索其中的字符串。它回显包含字符串的文件名数组。有没有办法做到这一点,可能是低内存使用率


非常感谢。

要实现类似的效果,我建议您阅读有关该类以及PHP的内容

下面是一个示例,说明如何读取给定目录($dir)的内容,并使用它搜索每个文件内容($contents)中出现的特定字符串:


这一条很容易在网上搜索
<?php

$dir = '.';

if (substr($dir, -1) !== '/') {
    $dir .= '/';
}

$matchedFiles = [];

$dirIterator = new \DirectoryIterator($dir);

foreach ($dirIterator as $item) {
    if ($item->isDot() || $item->isDir()) {
        continue;
    }

    $file = realpath($dir . $item->getFilename());

    // Skip this PHP file.
    if ($file === __FILE__) {
        continue;
    }

    $contents = file_get_contents($file);

    // Seach $contents for what you're looking for.
    if (strstr($contents, 'this is what I am looking for')) {
        echo 'Found something in ' . $file . PHP_EOL;
        $matchedFiles[] = $file;
    }
}

var_dump($matchedFiles);
<?php
$folderPath = '/htdocs/stock/tae';
$searchString = 'php';
$cmd = "grep -r '$searchString' $folderPath";
$output = array();
$files = array();
$res = exec($cmd, $output);
foreach ($output as $line) {
    $files[] = substr($line, 0, strpos($line, ':'));
}
print_r($files);