grep与

编程入门 行业动态 更新时间:2024-10-24 04:35:42
本文介绍了grep与-f一样在PHP中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

PHP中有这样一个函数,它在unix / linux系统中执行grep -f filename。 如果没有,PHP函数/工具将有助于为此创建定制的方法/函数。感谢!

解决方案

其实恕我直言,我会说这是以下内容:

$ result = preg_grep($ pattern,file($ path));

请参阅 preg_grep ­ Docs 和 file ­ Docs 。 $ b

如果您需要对一组文件执行该操作(递归),还可以使用 glob 和 foreach 或(递归) DirectoryIterator 或 GlobIterator ­ Docs ,不要忘记 RegexIterator ­ Docs 。

使用 SplFileObject 和 RegexIterator 的示例:

$ stream = new SplFileObject($ file); $ grepped = new RegexIterator($ stream,$ pattern); foreach($ grepped as $ line){ echo $ line; $ / code>

输出(所有行 $ file 包含$ pattern): $ p $ $ grepped = new RegexIterator($ stream,$ pattern); foreach($ grepped as $ line){ echo $ line; }

演示: eval.in/208699

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

See preg_grep­Docs and file­Docs.

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.

Example with SplFileObject and RegexIterator:

$stream = new SplFileObject($file); $grepped = new RegexIterator($stream, $pattern); foreach ($grepped as $line) { echo $line; }

Output (all lines of $file containing $pattern):

$grepped = new RegexIterator($stream, $pattern); foreach ($grepped as $line) { echo $line; }

Demo: eval.in/208699

更多推荐

grep与

本文发布于:2023-07-16 23:03:55,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:grep

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!