I have a directory of about 30,000 text files and I'd like to search inside each to find if they contain a specified text. How can I do this effectively in PHP?
+2
A:
grep
is your friend. You can use one of php's functions that allow execution of external commands to call it, if for some reason you need the result inside a PHP script.
Amber
2010-07-03 03:27:11
If he's really got 30k files, he would need to `grep -r [exp] folder-with-30k-files/` , or he's going to blow the system's ARG_MAX limit, where grep would just say "too many arguments".
Tim Post
2010-07-03 05:40:04
@Tim: Can always use `xargs` to feed filenames to it.
Amber
2010-07-03 06:40:19
A:
1) Use grep as mentioned above
2) Cache the search results so you don't have to search 30,000 text files for the same search term again.
John Conde
2010-07-03 04:16:43
A:
As the others have said, grep is your friend, do a system call for it.
cmendoza
2010-07-03 05:11:22
A:
Also, consider building a search index using something like Lucene: http://lucene.apache.org/java/docs/index.html
audiodude
2010-07-03 05:23:23
A:
are all the files going to stay as the same name? are any files going to be added, or is it set on 30,00? if all filename stay the same, and no files are going to be added, i would make an index (maybe index.txt), and explode that to get filenams for keywords.
tann98
2010-10-08 14:50:13