Page 1 of 1

using grep in php to create a search engine

Posted: Wed Oct 29, 2003 7:14 am
by mccommunity
I have a directory on my linux box that has html and word docs. Is there a way I can use grep to search through this directory for specicic words and pull only the documents that have that word in it? How would I do this using php?

Posted: Wed Oct 29, 2003 7:19 am
by Nay
It would be slow but I think you would open the directory (opendir()), read each file's contents and search for the specific word. If there is the word in the document, then store the file name into an array. Then after it has gone through all the files, output the filenames that has a match in it with a loop.

-Nay

???

Posted: Wed Oct 29, 2003 7:30 am
by mccommunity
I am new to all of this can you give me an example of what you mean.

Posted: Wed Oct 29, 2003 1:54 pm
by m3rajk
he means that since you're new and haven't bookmarked http://www.php.net nor would you necessarily know of functions everyone here, well i think it's safe to say everyone here, thinks you should bookmark that url, then use the function search to search for fopen, and then for preg_match and while....

i'm going to suggest that you think about this psuedo-code structure:

handle=open(directory);
while (you can grab another file){
check if the file matches what you want
if it does, add it to an array
}
close(handle)


btw: preg_matching really makes it easy to see if it has the extention(s) you want

Posted: Wed Oct 29, 2003 2:03 pm
by gite_ashish
one shortcut, if you folks don't mind much ;-)

Code: Select all

<?php

$word = "Linux";   // word to search
$opt = "-ilr";   //   ignore case, list files with matches, search recursive
$path = "/var/www/html/files";

$result = `grep $opt $word $path`;

echo $result;

?>

Posted: Wed Oct 29, 2003 2:29 pm
by Cruzado_Mainfrm
i have a better idea, make a script that reads all the documents in a folder, reads the data inside every doc and copies it to a db, and when a user comes looking for a file, his search is done in the db which in turns returns the file that has that data or keyword specified, you just have to run the script everytime you add/edit/delete a file... which i guess will not be that often

Posted: Wed Oct 29, 2003 2:32 pm
by m3rajk
i think that's very diff than both nay and i were pointing him to, specially since he was talking about opening directories and such. you showed him how to use a linux command...two very different ways to do the same thing... i guess it depends on how much he wants to learn about php on which he takes, the less he wants to learn now the more he should take yours, the more he wants to learn now the more he should look at plying with opening directories and the other things nay and i pointed him too (actually i just decided to follow nay's lead)