PHPには、unix/linuxシステムでgrep -f filenameを実行する関数がありますか? 何もない場合、このためにカスタマイズされたメソッド/関数を作成するのにどのPHP関数/ツールが役立つでしょうか?ありがとう!grep with -f like PHPで
7
A
答えて
9
実はIMHO、私はそれは次のようだと言うだろう:
$result = preg_grep($pattern, file($path));
preg_grep
Docsとfile
Docsを参照してください。
あなたはファイルのセットの上に(再帰的に)それを行うために必要がある場合は、そこglob
とforeach
または(Recursive
)DirectoryIterator
またはGlobIterator
DocsもあるとRegexIterator
Docsを忘れないように。
例SplFileObjectとRegexIterator有する:
$stream = new SplFileObject($file);
$grepped = new RegexIterator($stream, $pattern);
foreach ($grepped as $line) {
echo $line;
}
出力($パターンを含む$file
の全ライン):
$grepped = new RegexIterator($stream, $pattern);
foreach ($grepped as $line) {
echo $line;
}
2
ファイルを開くにはfile_get_contens()関数を、正規表現を使用してコンテンツを取得するにはpreg_match_all()関数を組み合わせることができます。
$file = file_get_contents ('path/to/file.ext');
preg_match_all ('regex_pattern_here', $file, $matches);
print_r ($matches);
+0
私は 'file_get_contents()'を使用していましたが、断続的に失敗します。 – FractalSpace
関連する問題
- 1. grep combine -fと-E
- 2. clojure like cond in F#
- 3. grep with greating with fswatch
- 4. grepとexpression with(*、*)
- 5. grep with string
- 6. grep -f here hereのドキュメント
- 7. r grep with or stat
- 8. grep with expression2 AFTER expression1
- 9. アクセス、クエリ "LIKE" with mask
- 10. Rがlike/grep条件に参加する
- 11. grep remote machine with]文字
- 12. Android WebView with Facebook Like機能
- 13. php jquery likeセレクタエンジン
- 14. 'grep -f'オプションですべての一致を取得
- 15. 'grep -q'が 'tail -f'で終了しない
- 16. php mysqli prepared statement LIKE
- 17. PHP検索システム - LIKEの使い方
- 18. vanity url with .htaccess with PHP
- 19. F#名前空間/モジュールとInterop with C#
- 20. Google Visualization with PHP
- 21. tail -f grep into cutが正しく動作しない
- 22. について 'grep -f':ファイルとパターンを一致させる
- 23. jquery/php likeボタンの問題
- 24. PHP MySQLのLIKE検索
- 25. php - mysqlフェッチ配列(LIKE%)
- 26. PHPで "Sudoku like" HTMLテーブルを生成
- 27. PHP Transaction-like SQLが必要ですか?
- 28. PHP facebookで注文するlike
- 29. .htaccess with PHP GET
- 30. Linux tail + grep + less
これは機能的には同等ですが、ファイル全体をメモリにロードしますが、コマンドラインのgrepはロードしません。 – cmc
@cmd:はい、メモリを節約したい場合は、[** SplFileObject **](http://php.net/SplFileObject)と[** RegexIterator **](http://php.net/ RegexIterator)。 – hakre
いいね! ...あなたはpreg_grep($ pattern、file($ path))のドロップイン置換である例を手にしたことはありませんか? – cmc