Forum Moderators: coopster

Message Too Old, No Replies

Basic Search Engine for Static Pages

Code and explaination

         

vincevincevince

1:50 pm on Sep 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This code is provided as is in answer to a number of recent questions about site search facilities. It IS suitable for non-database driven sites - which is what the majority of questions have been about.

Specification:
Searches for all files at a given directory level and subdirectories of the above recursively and parses them for a case insensitive exact text match before returning up to 50 results sorted by most frequent occourance of given text.

Suggested implementation:
Site search facility for sites containing uploaded html files, txt files, rtf files, or any other file type where the text to be searched is available in a plaintext mode.

Limitations:
Does not provide snippets without modification first, limited error checking, does not cache or index search results leading to search times getting longer with large amounts to search. Not able to search php database driven correctly.

Use:
Paste into a .php file and change lines 3 and 5 to the local directory an the global path as required.


<?php
//local path to files
$fdir="files";
//global path to files
$trueurl="http://www.yourdomain.com/";
?>
<FORM METHOD=POST>
Search <INPUT TYPE="text" NAME="q">
</FORM>
<?php
if ($_REQUEST[q]) search($_REQUEST[q]);

function search($term)
{
echo "<br>Searching for : $_REQUEST[q]<hr>";
global $fdir;
global $trueurl;
$term=" $term ";
global $files;
rec_load($fdir);
for ($a=0;$a<sizeof($files[path]);$a++)
{
$files[count][$a]=@substr_count(strtolower($raw = @implode("", file($files[path][$a]))),strtolower($term));
//echo $files[path][$a]." - ".$files[count][$a]."<br>";
}
for ($b=0;$b<50;$b++)
{
$max=0;
$val=0;
for ($a=0;$a<sizeof($files[path]);$a++)
{
if ($files[count][$a]>$val) {$max=$a;$val=$files[count][$a];}
}
$ranks[]=$max;
echo ($b+1)." - <a href=\"$trueurl".$files[path][$max]."\">".$files[path][$max]."</a><br>";
$files[count][$max]="0";
}
}

function rec_load($filesdir)
{
global $files;
$dh=opendir($filesdir);
while ($file=readdir($dh))
{
if ((!($file=="."))&&(!($file=="..")))
{
//echo "$filesdir/".$file."<br>";
if (!is_dir("$filesdir/".$file)) {$files[path][sizeof($files[path])+1]=$filesdir."/".$file;}
else rec_load($filesdir."/".$file);
}
}
return;
}
?>

jatar_k

3:14 pm on Sep 10, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



nice V3, I will have to find a little time to test it out.