翻出一篇老文章:php文本站內全文檢索

發表于:2007-07-01來源:作者:點擊數: 標簽:
? php /**************************************************** 原作者: uchinaboy 修改:lingshidao 特點:無需 mysql 支持;速度快;無需設置路徑,放在哪級目錄下,就搜索該目錄和子目錄;可以搜索一切文本類型的文件;顯示文件相關內容;關鍵詞自動高亮顯
<?php  
/****************************************************   
原作者: uchinaboy    
修改:lingshidao
特點:無需mysql支持;速度快;無需設置路徑,放在哪級目錄下,就搜索該目錄和子目錄;可以搜索一切文本類型的文件;顯示文件相關內容;關鍵詞自動高亮顯示。
修改內容:增加了自動分頁和風格設置文件。
搜索框代碼(如果放在search.php相同目錄下,無需修改):<form method="post" action="search.php"><input type="text" name="key" size=40 value="">                         
<input type="submit" value="檢索"></form>
****************************************************/  
require ("template.php");
echo "<p align=\"center\">";
echo "檢索結果";
echo "</p><hr>";
if (function_exists("set_time_limit") && !get_cfg_var(@#safe_mode@#)){
set_time_limit(600);}
function get_msg($path) {  
global $key, $i;  
$handle = opendir($path);  
while ($filename = readdir($handle)) {  
//echo $path."/".$filename."<br>";  
$newpath = $path."/".$filename;  
if (is_file($newpath)) {  
$fp = fopen($newpath, "r");  
$msg = fread($fp, filesize($newpath));  
fclose($fp);  
match_show($key, $msg, $newpath, $filename);  
}  
if (is_dir($path."/".$filename) && ($filename != ".") &&  ($filename != "..")) {  
//echo "<BR><BR><BR>".$newpath."<BR><BR><BR>";  
get_msg($path."/".$filename);  
}  
}  
closedir($handle);  
return $i;  
}  

function match_show($key, $msg, $newpath, $filename) {
  global $i;
  $key = chop($key);
  if($key) { $check_type = preg_match("/\.html?$/", $filename);
  if($check_type) {$title = getHtmlTitle($msg);}
    $msg = preg_replace("/<style>.+<\/style>/is", "", $msg);
    $msg = preg_replace("/<[^>]+>/", "", $msg);
    $value = preg_match("/.*$key.*/i", $msg, $res);
        if($value) {
     

      if($title) {$m = $title;} else {$m = $filename;}
      $i++;
      $link = $newpath;
      echo "$i.◆<a href=\"$link\">$m</a><BR><BR>";
    }
  }else {
    echo "請輸入關鍵詞";
    exit;
  }
}

function getHtmlTitle($msg) {
    
    /* Locate where <title> is located in html file. */
    $lBound = strpos($msg, @#<title>@#) + 7; //7 is the lengh of <title>.

    if ($lBound < 1)
        return;

    /* Locate where </TITLE> is located in html file. */
    $uBound = strpos($msg, @#</title>@#, $lBound);

    if ($uBound < $lBound)
        return;

    /* Clean HTML and PHP tags out of $title with the madness below. */
    $title = ereg_replace("[\t\n\r]", @#@#, substr($msg, $lBound, $uBound - $lBound));
    $title = trim(strip_tags($title));

    if (strlen($title) < 1) //A blank title is worthless.
        return;

    return $title;
}

$i = get_msg(".");
echo "<hr><p align=\"center\">";
echo " 已經搜索到了 $i 條信息";
?>  

原文轉自:http://www.anti-gravitydesign.com

国产97人人超碰caoprom_尤物国产在线一区手机播放_精品国产一区二区三_色天使久久综合给合久久97