前端

img sunthing

用PHP 做站内的搜索引擎

发表于2004/9/27 11:42:00  1220人阅读

分类: PHP 程序设计

如果您的网站是静态页面(用静态页面的好处不多说了),而你的虚拟主机以支持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);
}
$my_os=$HTTP_ENV_VARS["OS"];
$linux_doc_path=$_SERVER["DOCUMENT_ROOT"];
$my_path_info=$HTTP_ENV_VARS["PATH_INFO"];
$my_path_translated=$HTTP_ENV_VARS["PATH_TRANSLATED"] ;

function get_doc_path(){
 global $my_os,$linux_doc_path,$my_path_info,$my_path_translated;
 if(substr(strtolower($my_os),0,3)!="win"){
  $doc_path= $linux_doc_path;
  $doc_path_len=strlen($doc_path);
  if(substr($doc_path,-1)!="/"){
   $doc_path=$doc_path."/";
  }
 }else{
 
  $my_path_info_array=split("/",$my_path_info);
  $my_path_translated_array=split("////",$my_path_translated);
  $real_path=$my_path_translated_array[0]."//";
  for ($i=1;$i<count($my_path_translated_array);$i++){
   $real_path.=$my_path_translated_array[$i];
  } 
  $file_path="";
  for($i=0;$i<count($my_path_info_array);$i++){
   $file_path.=$my_path_info_array[$i];
  }
  $doc_path=str_replace($file_path,"",$real_path);
  $doc_path.="//";
 }
 return $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");//指定从什么目录搜索
//如果不指定目录则为
$doc_path=get_doc_path();$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>";
 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>";
}
?>

阅读全文
0 0

相关文章推荐

img
取 消
img