Website Search Script

28 Jan

Here goes i was asked to get a search function working for a website we do so here’s what i came up with inphp

function str_in_file($path,$search){
$count = 0;
$handle=opendir($path);
$filelist = array();
while ($file = readdir($handle)) { if ($file != “.” && $file != “..” && $type != “dir” && $file != “search.php” && $file != “response.htm” && $file != “index.php” && $file != “formmail.php”) {

$filen = fopen($file, “r”);
$file_content = fread($filen, filesize($file));

fclose($filen);

$file_content = strip_tags($file_content);

if(eregi($search, $file_content)){

$position = strpos($file_content, $search);
$pos = $position – 20;
$pos1 = “-10”;

$place = substr($file_content, $pos, 200);
//$place = strip_tags($place);
$place = str_replace($search, “$search“,$place);
$place = “…..”.$place.”…..”;
//print “position is $position
“;
$filet = explode(“.”,$file);

$filep = $filet[0];
$filea = strtoupper($filep);
$file = “$filea“;
$filelist[$count] = array (‘file’ => $file, ‘place’ => $place, ‘position’ => $position);

$count=$count +1;
//print “$count $file
“;
}
//read_dir($sub_dir);

}

}
return $filelist;

}

$path = “your/start/path”;
$search = $_POST[‘s’]; //from search form
$filelist = str_in_file($path,$search);

//print your results
foreach ($filelist as $value){
echo $value;
}

Let me know if you find it useful

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: