A very basic method would be to read each file into PHP and search through them with one of the string searching functions.
//loop through all filenames and for each one:
$contents = file_get_contents($filename) ;
if (strpos($contents, $keyword) !== false) {
//found a match!
}
However this is very inefficient, since you will have to do that file reading and searching every single time you perform a search.
That's why search engines create indexes of the entire files they know about in advance, and then just look into those indexes for the search keyword. If you want to look into that, you would need a separate script (say indexer.php) that will do something like this:
- loop through each file, getting its contents
- break those into words
- keep a record of unique words found in that file
- store that record in a database or file on disk
And have it run every now and then to update its index. Its index could for example look like this:
$words = array(
'mobile' => array('filename1.txt', 'filename2.txt'),
'answer' => array('filename3.txt', 'filename5.txt', 'filename6.txt'),
//...
);
Then, when you are searching for a certain keyword, you just need to load the index from your index file or database and see which filenames that word is found in.
if (isset($words[$keyword])) {
echo "Found in: " . join(', ', $words[$keyword]) ;
}
And there you have a very simplistic way of doing something like this. Further down the road you can store the index into a database, count how many times a word is found in each file to provide more relevant results, etc etc.