using grep in php to create a search engine

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mccommunity
Forum Commoner
Posts: 62
Joined: Mon Oct 07, 2002 8:55 am

using grep in php to create a search engine

Post 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?
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post 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
mccommunity
Forum Commoner
Posts: 62
Joined: Mon Oct 07, 2002 8:55 am

???

Post by mccommunity »

I am new to all of this can you give me an example of what you mean.
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post 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
User avatar
gite_ashish
Forum Contributor
Posts: 118
Joined: Sat Aug 31, 2002 11:38 am
Location: India

Post 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;

?>
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post 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
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post 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)
Post Reply