Page 1 of 1
Reading files in directory
Posted: Tue Sep 12, 2006 9:58 pm
by winsonlee
How can i program in a way that the php script able ot read a keyword and list all the files that contains the keyword ??
There is a page that that contain an input box that allows user to key in the keyword and after the user click on the button, it will try matching all the files that contain the keyword on the drive and list it on a page. Will it be possible to make the search function works just like the windows search function ??
Posted: Tue Sep 12, 2006 9:59 pm
by Luke
glob()
Posted: Tue Sep 12, 2006 10:58 pm
by akimm
I thought glob listed files in a directory, can it actually search the entirity of the file, I think he might need something different than glob()
Posted: Tue Sep 12, 2006 11:06 pm
by Luke
oh my mistake... I thought he meant search for a keyword in the file name

(guess I should have read the whole post)
Posted: Tue Sep 12, 2006 11:10 pm
by feyd
depending on the size of the files and the amount of RAM php is allowed to use,
file()/
file_get_contents() may be of interest. If the files are too large,
fopen()/
fread() and some clever
substr() calls will be in order.
Well
Posted: Tue Sep 12, 2006 11:17 pm
by akimm
Get someone better hopefully to correspond, or adjust this code, but basically perl regular expressions are good *I think* for your purpose.
Code: Select all
<?php
##b in front of web indicates specific search, not partial, words like webbling would not be found
##after the preg_ function I am kind of experimenting, because I don't know these functions well yet.
##assuming $_POST['search'] is the value for a search form..
if (preg_match("/\bweb\b/i", $_POST['search'] )) {
echo "A match was found.";
} else {
echo "A match was not found.";
}
##below it searches in-between those quotes for that phrase.
if (preg_match("/\bweb\b/i", ".")) {
echo "A match was found.";
} else {
echo "A match was not found.";
}
?>