Zend - The PHP Company




Miscellaneous

Add Code


Meta Search 1.0  

Type: application
Added by: lysander
Entered: 15/10/2002
Last modified: 31/10/2001
Rating: - (fewer than 3 votes)
Views: 4953
MetaSearch is a variation of SiteSearch 1.2 which only searches Meta keyword tags instead of all the text on the page.


<!-- Begin Search -->
<?php 
//MetaSearch v1.0 - Sheridan Saint-Michel (Lysander@onlychildclub.com)
//MetaSearch is a variation of SiteSearch 1.2 which only searches 
//Meta keyword tags instead of all the text on the page.

//HTML Comments for sake of debugging if included or required by another script

//Check to see if search is included in parent document
if (!isset($parent) || $parent==0
{
?>

<HTML>
<Head>
<Title>Search</Title>
</Head>
<Body>
<?php 

?>

  <Form Action="<?=$PHP_SELF?>" Method=Post>
    <?php echo "<Input Type=Text Name=keyword Value="$keyword">"?>
    <Input Type=Submit Value="Search">
    <Input Type=RESET Value="Reset to Last Search">
  </Form>

<?php
// -- 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/"   
);   


//Filters for files that should not be included in search results
//These must be set in a PERL style regex

$filter = array(
"/^.$/",    //Filter out . file
"/^..$/",  //Filter out .. file
"/^./"      //Filter out Unix hidden files (anything that starts with .)
);

/* Examples:
To only display .htm or .html add
"/[^(.htm|.html)]$/"   //Filter out anything not ending in .htm or .html

To prevent the script from scanning through gifs and jpgs add
"/(.gif|.jpg)$/"       //Filter out .gif and .jpg files
*/

// -- End Environment Variables -- //

Function Check_Filter($filter,$filename)
{
//Checks filename against filters and returns true if no match
  
$retVal 1;
  for(
$i=0;$i<count($filter);$i++)
  {
    if(
preg_match($filter[$i],$filename))
      
$retVal 0;
  }
  
  return 
$retVal;
}

function 
Get_Filenames($directory,$filter

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

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

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);
    
    
//Find title of File  
    
preg_match("|<Title>(.+)</Title>|Ui"$contents$regs );
    
$title $regs[1];
    
    
//Use Filename if no Title Tag
    
if(!$title)
      
$title $filename;
     
    
preg_match("|<meta[^>]*description[^>]*content="([^>]+)"[^>]*>|Ui",$contents$regs);
      
$desc $regs[1];

    
preg_match("|<meta[^>]*keywords[^>]*content="([^>]+)"[^>]*>|Ui",$contents$regs);
      
$contents $regs[1];

    
//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 (trim($contents[$k]), $keywords[$j])) 
        {
          
$match++;
          break;
        }
      }
    }
   
   if (
$match == count($keywords) )
   {
     
$counter count($retVal);
       
$retVal[$counter][0] = $filename;
       
$retVal[$counter][1] = "$title - $desc";
   }
  }
  return 
$retVal;
}

// -- MAIN --

//Make sure keyword is present and contains at least one non-whitespace character
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,$filter);
    
$found Keyword_Check($filenames,$keywords);
  
    
//add any pages with keywords in current directory to array
    
for($i 0;$i count($found); $i++)
    {
      
$fileloc "$val".$found[$i][0];
      
$counter count($pages);
      
$pages[$counter][0] = $fileloc;
      
$pages[$counter][1] = $found[$i][1];;
    }
  }

  
$numfound count($pages);

  echo 
"<HR>n";
  echo 
"<Font Color=Blue>$numfound pages matching your query were found</Font>";
  echo 
"<HR>";
  for (
$i 0$i count($pages); $i++)
  {
    
$fileloc $pages[$i][0];
    
$title $pages[$i][1];
    echo 
"<A HREF="$fileloc">$title</A><BR>";
  }
  echo 
"<BR>";
}
if (!
$parent) {
?>    
</Body>
</HTML>
<?php ?>
<!-- End Search -->


Usage Example




Rate This Script





Search



This Category All Categories