«
php文本站内全文检索

时间:2008-5-31    作者:Deri    分类: 分享


   <p>  <code><?php&#160;<br />/****************************************************&#160;<br />原作者: uchinaboy &#160;<br />修改:lingshidao<br />特点:无需mysql支持;速度快;无需设置路径,放在哪级目录下,就搜索该目录和子目录;可以搜索一切文本类型的文件;显示文件相关内容;关键词自动高亮显示。<br />修改内容:增加了自动分页和风格设置文件。<br />搜索框代码(如果放在search.php相同目录下,无需修改):<form method="post" action="search.php"><input type="text" name="key" size=40 value="">           &#160;<br /><input type="submit" value="检索"></form><br />****************************************************/&#160;<br />require ("template.php");<br />echo "<p align="center">";<br />echo "检索结果";<br />echo "</p><hr>";<br />if (function_exists("set_time_limit") && !get_cfg_var('safe_mode')){<br />set_time_limit(600);}<br />function get_msg($path) {&#160;<br />global $key, $i;&#160;<br />$handle = opendir($path);&#160;<br />while ($filename = readdir($handle)) {&#160;<br />//echo $path."/".$filename."<br>";&#160;<br />$newpath = $path."/".$filename;&#160;<br />if (is_file($newpath)) {&#160;<br />$fp = fopen($newpath, "r");&#160;<br />$msg = fread($fp, filesize($newpath));&#160;<br />fclose($fp);&#160;<br />match_show($key, $msg, $newpath, $filename);&#160;<br />}&#160;<br />if (is_dir($path."/".$filename) && ($filename != ".") && ($filename != "..")) {&#160;<br />//echo "<BR><BR><BR>".$newpath."<BR><BR><BR>";&#160;<br />get_msg($path."/".$filename);&#160;<br />}&#160;<br />}&#160;<br />closedir($handle);&#160;<br />return $i;&#160;<br />} <br />function match_show($key, $msg, $newpath, $filename) {<br /> 
      global $i;<br /> 
      $key = chop($key);<br /> 
      if($key) { $check_type = preg_match("/.html?$/", $filename);<br /> 
      if($check_type) {$title = getHtmlTitle($msg);}<br />  
      $msg = preg_replace("/<style>.+</style>/is", "", $msg);<br />  
      $msg = preg_replace("/<[^>]+>/", "", $msg);<br />  
      $value = preg_match("/.*$key.*/i", $msg, $res);<br />    
      if($value) {<br />if($title) {$m = $title;} else {$m = $filename;}<br />   
        $i++;<br />   
        $link = $newpath;<br />   
        echo "$i.◆<a href="$link">$m</a><BR><BR>";<br />  
        }<br /> 
        }else {<br />  
        echo "请输入关键词";<br />  
        exit;<br /> 
        }<br />}<br />function getHtmlTitle($msg) {<br />           &#160;<br />  
          /* Locate where <title> is located in html file. */<br />  
          $lBound = strpos($msg, '<title>') + 7; //7 is the lengh of <title>.<br />if ($lBound < 1)<br />    
            return;<br />/* Locate where </TITLE> is located in html file. */<br />  
              $uBound = strpos($msg, '</title>', $lBound);<br />if ($uBound < $lBound)<br />    
                return;<br />/* Clean HTML and PHP tags out of $title with the madness below. */<br />  
                  $title = ereg_replace("[tnr]", '', substr($msg, $lBound, $uBound - $lBound));<br />  
                  $title = trim(strip_tags($title));<br />if (strlen($title) < 1) //A blank title is worthless.<br />    
                    return;<br />return $title;<br />}<br />$i = get_msg(".");<br />echo "<hr><p align="center">";<br />echo " 已经搜索到了 $i 条信息";<br />?></code></p>