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 或 GlobIteratorDocs 并且不要忘记 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 GlobIteratorDocs and not to forget the RegexIteratorDocs.
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 中一样
基础教程推荐
- 如何替换eregi() 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
