用PHP 做站内的搜索引擎

类别:编程语言 点击:0 评论:0 推荐:

如果您的网站是静态页面(用静态页面的好处不多说了),而你的虚拟主机以支持PHP,那么用PHP 做站内的搜索引擎是个不错的选择!
源代码如下:

<?PHP
/****************************************************  
文件名:search.php
源作者:不详
修改者:sunthing.xie/sanxin.xie 
调用方法:
<form method="post" action="search.php">
<input type="text" name="key" size=40 value="">                        
<input type="submit" value="search">
</form>
sunthing 的主页:http://gems.51.net
三新期盼欢迎和大家一起交流
****************************************************/ 
if (function_exists("set_time_limit") && !get_cfg_var('safe_mode')){
 set_time_limit(600);
}
$doc_path= $_SERVER["DOCUMENT_ROOT"];
$doc_path_len=strlen($doc_path);
if(substr($doc_path,-1)!="/"){
 $doc_path=$doc_path."/";
}

function get_msg($path){ 
 global $key, $i;
 $handle = opendir($path); 
 while ($filename = readdir($handle)) { 
  $newpath = $path."/".$filename; 
  if (is_file($newpath)) { 
   if (substr($newpath,0,3)!='./.'){
    $fp = fopen($newpath, "r"); 
    $msg = fread($fp, filesize($newpath)); 
    fclose($fp);
    match_show($key, $msg, $newpath, $filename); 
   }
  }
  if (is_dir($path."/".$filename) && ($filename != ".") &&  ($filename != "..")) { 
   get_msg($path."/".$filename); 
  } 
 } 
 closedir($handle); 
 return $i; 

function match_show($key, $msg, $newpath, $filename) {
 global $ar, $i,$doc_path;
 $key = chop($key);
 if($key){
     $msg = preg_replace("/<style>.+<\/style>/is", "", $msg);
     $msg = str_replace(" ", " ", $msg);
     $msg = preg_replace("/<[^>]+>/", "", $msg);
     $value = preg_match("/.*$key.*/i", $msg, $res);
     if($value) {
      $res[0] = preg_replace("/$key/i", "<FONT COLOR=\"red\">$key</FONT>", $res[0]);
      $i++;
      $link =str_replace($doc_path,"",$newpath);
      $ar[] = "<font size=\"3\">$i.</font><a href=\"/$link\"><FONT face=\"verdana\" size=\"2\">$filename</FONT></a><BR><BR><FONT face=\"verdana\" size=\"2\">".$res[0]."</FONT><BR><br>";
     }
   }else {
     echo "Please Enter Keywords";
     exit;
 }
}
 /****************开始搜索部分************************/
//$i = get_msg("detailedpage");//指定从什么目录搜索
$i=get_msg($doc_path."demo");
if (empty($page)) $page=1;
 $maxresult=($page*10);
 $resultcount = count($ar);
if ($resultcount==0){
 echo" <font color=\"red\"><h2>Sorry, no matches were found. </font></h2> <br>Please try again using different search words. <br><br>";
}else{
 if($resultcount%10==0){
  $maxpageno=$resultcount/10;
 }else{
  $maxpageno=floor($resultcount/10)+1;
 }
 if ($page>$maxpageno){
  $page=$maxpageno;
  $pagemax=$resultcount-1;
  $pagemin=max(0,$result_count-10);
 }elseif($page==1){
  $pagemin=0; $pagemax=min($result_count-1,10-1);
 }else{
  $pagemin=min($resultcount-1,10*($page-1));
  $pagemax=min($resultcount-1,$pagemin+10-1);
 }
 $maxresult=min($maxresult,$resultcount);
 echo "<p align=\"center\">";
 echo "<br><h1 align=center>Search Results</h1>";
 echo "</p><hr noshade size='1' color='#336699'>";
 for ($i=max(0,$maxresult-10); $i<$maxresult; $i++) {
  print $ar[$i];
 }
 echo "<hr size='1' noshade><p align=\"center\">";
 echo " <font face=\"verdana\" size=\"2\">Search $resultcount info </font>";
      $nextpage=$page+1;
      $previouspage=$page-1;
 echo " --- [ <a href='search.php?key=$key&page=".($page-1)."'  target='_self'> <font face=\"verdana\" size=\"2\">Forward 10  <font face=\"verdana\"></a> ]";
 echo "[ <a href='search.php?key=$key&page=".($page+1)."'  target='_self'> <font face=\"verdana\" size=\"2\">Next 10  <font face=\"verdana\"></a> ] </p>";
}
?>

本文地址:http://com.8s8s.com/it/it27075.htm