用PHP程序为自己网站打造一个搜索引擎

字体大小: 中小 标准 ->行高大小: 标准
一、设计搜索表单 

在网站的根目录下建个search.htm,内容如下 


以下为引用的内容:
<html> 
<head> 
<title>搜索表单</title> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 
</head> 
<body bgcolor="#FFFFFF" text="#000000"> 
<form name="form1" method="post" action="search.php"> 
<table width="100%" cellspacing="0" cellpadding="0"> 
<tr> 
<td width="36%"> 
<div align="center"> 
<input type="text" name="keyword"> 
</div> 
</td> 
<td width="64%"> 
<input type="submit" name="Submit" value="搜索"> 
</td> 
</tr> 
</table> 
</form> 
</body> 
</html>  


二、搜索程序 

再在根目录下建个search.php 的文件,用来处理search.htm表单传过来的数据.内容如下 


以下为引用的内容:
<?php 
//获取搜索关键字 
$keyword=trim($_POST[“keyword”]); 
//检查是否为空 
if($keyword==””){ 
echo”您要搜索的关键字不能为空”; 
exit;//结束程序 
} 
?>  


这样如果访问者输入的关键字为空时,可以做出提示。下面是遍历所有文件。 

我们可以用递归的方法遍历所有的文件,可以用函数opendir,readdir,也可以用PHP Directory的类。我们现在用前者。


以下为引用的内容:
<?php 
//遍历所有文件的函数 
function listFiles($dir){ 
$handle=opendir($dir); 
while(false!==($file=readdir($handle))){ 
if($file!="."&&$file!=".."){ 
//如果是目录就继续搜索 
if(is_dir("$dir/$file")){ 
listFiles("$dir/$file"); 
} 
else{ 
//在这里进行处理 
} 
} 
} 
} 
?>  


在红字的地方我们可以对搜索到的文件进行读取,处理下面就是读取文件内容,并检查内容中是否含有关键字$keyword,如果含有就把文件地址赋给一个数组。 


以下为引用的内容:
<?php 
//$dir是搜索的目录,$keyword是搜索的关键字 ,$array是存放的数组 
function listFiles($dir,$keyword,&$array){ 
$handle=opendir($dir); 
while(false!==($file=readdir($handle))){ 
if($file!="."&&$file!=".."){ 
if(is_dir("$dir/$file")){ 
listFiles("$dir/$file",$keyword,$array); 
} 
else{ 
//读取文件内容 
$data=fread(fopen("$dir/$file","r"),filesize("$dir/$file")); 
//不搜索自身 
if($file!=”search.php”){ 
//是否匹配 
if(eregi("$keyword",$data)){ 
$array[]="$dir/$file"; 
} 
} 
} 
} 
} 
} 
//定义数组$array 
$array=array(); 
//执行函数 
listFiles(".","php",$array); 
//打印搜索结果 
foreach($array as $value){ 
echo "$value"."<br> "; 
} 
?>  


现在把这个结果和开头的一段程序结合起来,输入一个关键字,然后就会发现你的网站中的相关结果都被搜索出来了。我们现在在把它完善一下。 

1、列出内容的标题 

把 


以下为引用的内容:

if(eregi("$keyword",$data)){ 
$array[]="$dir/$file"; 
} 

改成 

if(eregi("$keyword",$data)){ 
if(eregi("<title>(.+)</title>",$data,$m)){ 
$title=$m["1"]; 
} 
else{ 
$title="没有标题"; 
} 
$array[]="$dir/$file $title"; 
} 
 


原理就是,如果在文件内容中找到<title>xxx</title>,那么就把xxx取出来作为标题,如果找不到那么就把标题命名未”没有标题”。 

2、只搜索网页的内容的主题部分。 

做网页时一定会有很多html代码在里面,而这些都不是我们想要搜索的,所以要去除它们。我现在用正则表达式和strip_tags的配合,并不能把所有的都去掉。 

把 


以下为引用的内容:

$data=fread(fopen("$dir/$file","r"),filesize("$dir/$file")); 
//不搜索自身 
if($file!=”search.php”){ 
//是否匹配 
if(eregi("$keyword",$data)){ 
 


改为 


以下为引用的内容:

$data=fread(fopen("$dir/$file","r"),filesize("$dir/$file")); 
if(eregi("<body([^>]+)>(.+)</body>",$data,$b)){ 
$body=strip_tags($b["2"]); 
} 
else{ 
$body=strip_tags($data); 
} 
if($file!="search.php"){ 
if(eregi("$keyword",$body)){ 
 


3、标题上加链接 


以下为引用的内容:

foreach($array as $value){ 
echo "$value"."<br> "; 
} 
改成 

foreach($array as $value){ 
//拆开 
list($filedir,$title)=split(“[ ]”,$value,”2”); 
//输出 
echo "<a href=$filedir>$value</a>"."<br> "; 
} 
 


4、防止超时 

如果文件比较多,那么防止PHP执行时间超时是必要的。可以在文件头加上 


以下为引用的内容:

set_time_limit(“600”); 
 
以秒为单位,所以上面是设10分钟为限。 

所以完整的程序就是 


以下为引用的内容:
<?php 
set_time_limit("600"); 
//获取搜索关键字 
$keyword=trim($_POST["keyword"]); 
//检查是否为空 
if($keyword==""){ 
echo"您要搜索的关键字不能为空"; 
exit;//结束程序 
} 
function listFiles($dir,$keyword,&$array){ 
$handle=opendir($dir); 
while(false!==($file=readdir($handle))){ 
if($file!="."&&$file!=".."){ 
if(is_dir("$dir/$file")){ 
listFiles("$dir/$file",$keyword,$array); 
} 
else{ 
$data=fread(fopen("$dir/$file","r"),filesize("$dir/$file")); 
if(eregi("<body([^>]+)>(.+)</body>",$data,$b)){ 
$body=strip_tags($b["2"]); 
} 
else{ 
$body=strip_tags($data); 
} 
if($file!="search.php"){ 
if(eregi("$keyword",$body)){ 
if(eregi("<title>(.+)</title>",$data,$m)){ 
$title=$m["1"]; 
} 
else{ 
$title="没有标题"; 
} 
$array[]="$dir/$file $title"; 
} 
} 
} 
} 
} 
} 
$array=array(); 
listFiles(".","$keyword",$array); 
foreach($array as $value){ 
//拆开 
list($filedir,$title)=split("[ ]",$value,"2"); 
//输出 
echo "<a href=$filedir target=_blank>$title </a>"."<br> "; 
} 
?>  


到此为止,你已经做好了自己的一个搜索引擎,你也可以通过修改内容处理部分来改进它,可以实现搜索标题,或者搜索内容的功能。也可以考虑分页。这些都留给你自己吧。 

这里说明一下用preg_match代替eregi,会快很多。这里只是为了通俗易懂,所以使用了常用的eregi。

此文章由 http://www.ositren.com 收集整理 ,地址为: http://www.ositren.com/htmls/1246.html