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_grepDocs and fileDocs.
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.
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与
发布评论