我正在寻找最快的方法来搜索某些文件夹结构中的字符串.我知道我可以使用file_get_contents从文件中获取所有内容,但我不确定是否快速.也许已经有一些解决方案可以快速运行.我正在考虑使用scandir获取所有文件和file_get_contents来读取它的内容并使用strpos来检查字符串是否存在.
你认为有更好的方法吗?
或者也许尝试使用php exec和grep?
提前致谢!
hoh*_*ner 17
您的两个选项是DirectoryIterator或glob:
$string = 'something';
$dir = new DirectoryIterator('some_dir');
foreach ($dir as $file) {
    $content = file_get_contents($file->getPathname());
    if (strpos($content, $string) !== false) {
        // Bingo
    }
}
$dir = 'some_dir';
foreach (glob("$dir/*") as $file) {
    $content = file_get_contents("$dir/$file");
    if (strpos($content, $string) !== false) {
        // Bingo
    }
}
在性能方面,您始终可以非常轻松地计算代码的实时速度或查找内存使用情况.对于较大的文件,您可能希望使用替代方法file_get_contents.
| 归档时间: | 
 | 
| 查看次数: | 17735 次 | 
| 最近记录: |