原始碼
<?php
/*
站內全文檢索搜尋引擎
作者:yannan
*/
function tree($directory)
{
global $string;
$mydir=dir($directory);
while($file=$mydir->read())
{
if((is_dir("$directory/$file")) AND ($file!=".") AND ($file!=".."))
{
tree("$directory/$file");
}
else
{
//檢查檔案類型,只搜尋.php/html/htm檔案
if((strstr($file,".php")==".php")||(strstr($file,".html")==".html")||(strstr($file,".htm")==".htm"))
{
//開啟檔案
if(!($myfile=fopen($directory."/".$file,"r")))
{
print("file could not be opened");
exit;
}
//搜尋檔案內容
while(!feof($myfile))
{
//read a line from the file
$myline=fgets($myfile,500);
if(ereg($string,$myline))
{
//輸出結果
$path=substr($directory,2);
print("found <font color=\"ff00cc\">$string</font>");
print("in <a href=\"");
print($path."/".$file);
print("\">");
print($directory."/".$file);
print("</a><br>\n");
print(strip_tags($myline));
print("<br>\n");
}
} //endwhile of out put the file
fclose($myfile);
} //endif
} //endelse
} //endwhile
$mydir->close();
} //endfunction
//start the program
print("<form method=\"post\">\n");
print("搜尋索引鍵:");
print("<input type=\"text\" name=\"string\" size=\"30\">\n");
print("<input type=\"submit\" value=\"submit\"><br>\n");
print("</form>");
if((isset($string)) AND ($string!=""))
{
$root=".";
tree($root);
}
?>