grep with -f like in PHP(使用 -f 的 grep 就像在 PHP 中一样)
问题描述
PHP 中是否有这样的函数在 unix/linux 系统中执行 grep -f filename.如果没有,什么 PHP 函数/工具将有助于为此创建自定义方法/函数.谢谢!
Is there such a function in PHP that does grep -f filename in unix/linux systems. If there is none, what PHP functions/tools would help in creating a customised method/function for this. Thanks!
推荐答案
实际上恕我直言,我想说的是以下内容:
Actually IMHO, I'd say it's the following:
$result = preg_grep($pattern, file($path));
参见preg_grep
文档 和 file
文档.
如果您需要对一组文件(递归地)执行此操作,还有 glob
和 foreach
或 (Recursive
)DirectoryIterator
或 GlobIterator
Docs 并且不要忘记 RegexIterator
文档.
If you need to do that (recursively) over a set of files, there is also glob
and foreach
or the (Recursive
)DirectoryIterator
or the GlobIterator
Docs and not to forget the RegexIterator
Docs.
SplFileObject 和 RegexIterator 的示例:
$stream = new SplFileObject($file);
$grepped = new RegexIterator($stream, $pattern);
foreach ($grepped as $line) {
echo $line;
}
输出(所有包含 $pattern 的 $file
行):
Output (all lines of $file
containing $pattern):
$grepped = new RegexIterator($stream, $pattern);
foreach ($grepped as $line) {
echo $line;
}
演示:https://eval.in/208699
这篇关于使用 -f 的 grep 就像在 PHP 中一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 -f 的 grep 就像在 PHP 中一样


- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- PHP - if 语句中的倒序 2021-01-01