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 ??
Reading files in directory
Moderator: General Moderators
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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.
- akimm
- Forum Contributor
- Posts: 460
- Joined: Thu Apr 27, 2006 10:50 am
- Location: Ypsilanti Michigan, formally Clipsburgh
Well
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.";
}
?>