Zend - The PHP Company




Miscellaneous

Add Code


SiteSearch 1.1  

Type: application
Added by: Lysander
Entered: 07/02/2001
Last modified: 02/12/2001
Rating: ***** (7 votes)
Views: 10679
This app lets endusers search your site for keywords. You specify which directories should be included in the search. Version 1.1 Fixed a bug where if two words were only seperated by HTML Tags they were placed into only one string.


<?php 

//Sitesearch v1.1 - Sheridan Saint-Michel

//Begin Environment Variables.  You need to set these!//

//List of directories to be included in the search.
//Usage "path" => "url" (path to directory and URL which corresponds to directory)
//remember to include the trailing / on the URL

$directories = array( 
"/home/mysite/HTML" => "http://www.mysite.com/"
"/home/mysite/HTML/sales" => "http://www.mysite.com/sales/"
"/home/mysite/HTML/products" => "http://www.mysite.com/products/" 
); 

//End Environment Variables// 

Function Keyword_Check($filenames,$keywords)
{
  
  for(
$i 0$i count($filenames); $i++)     
  {
    
$filename $filenames[$i];
    
$match 0;
    
$fd fopen($filename"r");
    
$contents fread($fdfilesize ($filename));
    
fclose($fd);
    
//Remove HTML Tags before searching
        
$search = array ("'<script[^>]*?>.*?</script>'si",  // Strip out javascript
                         
"'<[/!]*?[^<>]*?>'si",  // Strip out html tags
                         
"'([rn])[s]+'",  // Strip out white space
                         
"'&(quot|#34);'i",  // Replace html entities
                         
"'&(amp|#38);'i",
                         
"'&(lt|#60);'i",
                         
"'&(gt|#62);'i",
                         
"'&(nbsp|#160);'i",
                         
"'&(iexcl|#161);'i",
                         
"'&(cent|#162);'i",
                         
"'&(pound|#163);'i",
                         
"'&(copy|#169);'i",
                         
"'&#(d+);'e");  // evaluate as php
        
        
$replace = array ("",
                          
" ",
                          
"\1",
                          
""",
                          "
&",
                          "
<",
                          "
>",
                          " ",
                          chr(161),
                          chr(162),
                          chr(163),
                          chr(169),
                          "
chr(\1)");
        
    
$contents = preg_replace ($search$replace$contents);
    
$contents = preg_replace ("/W/", " ", $contents);
    
$contents = preg_replace ("/s+/", " ", $contents);

    //Seperate Each Word into an Array Element and Compare to Keywords
    
$contents = explode(" ", $contents);
    
$j = 0;
    for(
$j = 0; $j < count($keywords); $j++)
    {
      for(
$k = 0; $k < count($contents); $k++)
      {
        //compare contents with each keyword
        if (!strcasecmp (
$contents[$k]$keywords[$j])) 
        {
          
$match++;
          break;
        }
      }
    }
   
   if (
$match == count($keywords) )
    
$retVal[count($retVal)] = $filename;
  }
  return 
$retVal;
}

function Get_Filenames(
$directory

  //Load Directory Into Array 
  
$handle=opendir($directory); 
  while (
$file = readdir($handle))
  {
    if (
$file != "." && $file != ".." && !is_dir($file)) 
      
$retVal[count($retVal)] = $file
  }

  //Clean up and sort 
  closedir(
$handle); 
  sort(
$retVal); 
  return 
$retVal
}

if (isset(
$keyword) && preg_match("|S+|",$keyword))
{
  
$keywords = explode(" ", $keyword);
  
$pages = array();
  while (list (
$key$val) = each ($directories)) 
  {
    
$directory = $key;
    chdir(
$directory) or die("Directory $directory Not found");
    
$filenames = Get_Filenames($directory);
    
$found = Keyword_Check($filenames,$keywords);
  
    //add any pages with keywords in current directory to array
    for(
$i = 0;$i < count($found); $i++)
    {
      
$add = "$val$found[$i]";
      
$pages[count($pages)] = $add;
    }
  }
$numfound = count($pages); 

?>

<HTML> 
<Head> 
<Title>Website Search</Title> 
</Head> 
<?php } ?> 
<Body> 
  <Form Action=search.php> 
    <Input Type=Text Name=keyword Value="
<?=$keyword?>";> 
    <Input Type=Submit Value=Search> 
    <Input Type=Reset Value="New Search"> 
<?php  
if ( isset($keyword) ) 

  echo 
"<HR>n"
  echo 
"<Font Color=Blue>$numfound pages matching your query were found</Font>"
  echo 
"<HR>"
  for (
$i 0$i count($pages); $i++) 
    echo 
"<A HREF="$pages[$i]">$pages[$i]</A><BR>"

?>     
</Form> 
</Body> 
</HTML>


Usage Example


See the example


Rate This Script





Search



This Category All Categories