问题
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?
回答1:
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.
回答2:
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.
回答3:
As the others have said, grep is your friend, do a system call for it.
回答4:
Also, consider building a search index using something like Lucene: http://lucene.apache.org/java/docs/index.html
回答5:
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.
来源:https://stackoverflow.com/questions/3170201/php-to-search-inside-30-000-text-files