<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[阿Tim日志]]></title> 
<link>https://atim.cn/index.php</link> 
<description><![CDATA[专业的php开发者.开发团队的带队人]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[阿Tim日志]]></copyright>
<item>
<link>https://atim.cn/post//</link>
<title><![CDATA[做个自己站内搜索引擎]]></title> 
<author>bkkkd &lt;partybase@gmail.com&gt;</author>
<category><![CDATA[开发应用]]></category>
<pubDate>Sun, 05 Feb 2006 01:17:08 +0000</pubDate> 
<guid>https://atim.cn/post//</guid> 
<description>
<![CDATA[ 
	<div><font size="2">  朋友用dreamweaver做了一个网站，没有动态的内容，只是一些个人收藏的文章，个人介绍等等。现在内容比较多了，想叫我帮他做一个搜索引擎。说实在的，这是一个不难的问题，于是就随手做了一个。现在我在其它论坛上也看到有人想做这个，于是就想说说这方面的知识，重在了解一下方法。<br /><br />写程序前先要想好一个思路，下面是我的思路，可能谁有更好的，但注意这只是一个方法问题 ：遍历所有文件  读取内容  搜索关键字,如果匹配就放入一个数组  读数组。在实现这些步骤之前，我假定你的网页都是标准的，就是有标题(&lt;title&gt;&lt;/title&gt;)，也有(&lt;bod *&gt;&lt;/body&gt;),如果你是用dreamweaver或者frontpage设计的，那么除非你故意删掉，它们都在存在的。下面就让我们一步步来完成并在工程中改善这个搜索引擎。<br /><br />一,设计搜索表单<br />在网站的根目录下建个search.htm，内容如下<br /></font></div><div><table style="BORDER-RIGHT: #cccccc 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: #cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellspacing="0" cellpadding="6" width="95%" align="center" border="0"><tbody><tr><td style="WORD-WRAP: break-word" bgcolor="#f3f3f3"><font size="2"><font style="FONT-WEIGHT: bold; COLOR: #990000">以下是引用片段：</font><br />&lt;html&gt;<br />&lt;head&gt;<br />&lt;title&gt;搜索表单&lt;/title&gt;<br />&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=gb2312&quot;&gt;<br />&lt;/head&gt;<br /><br />&lt;body bgcolor=&quot;#FFFFFF&quot; text=&quot;#000000&quot;&gt;<br />&lt;form name=&quot;form1&quot; method=&quot;post&quot; action=&quot;search.php&quot;&gt;<br />  &lt;table width=&quot;100%&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot;&gt;<br />    &lt;tr&gt; <br />      &lt;td width=&quot;36%&quot;&gt; <br />        &lt;div align=&quot;center&quot;&gt;<br />          &lt;input type=&quot;text&quot; name=&quot;keyword&quot;&gt;<br />        &lt;/div&gt;<br />      &lt;/td&gt;<br />      &lt;td width=&quot;64%&quot;&gt;<br />        &lt;input type=&quot;submit&quot; name=&quot;Submit&quot; value=&quot;搜索&quot;&gt;<br />      &lt;/td&gt;<br />    &lt;/tr&gt;<br />  &lt;/table&gt;<br />&lt;/form&gt;<br />&lt;/body&gt;<br />&lt;/html&gt;<br /></font></td></tr></tbody></table></div><div><br /><font size="2">二，搜索程序<br />再在根目录下建个search.php 的文件，用来处理search.htm表单传过来的数据.内容如下<br /></font></div><div><table style="BORDER-RIGHT: #cccccc 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: #cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellspacing="0" cellpadding="6" width="95%" align="center" border="0"><tbody><tr><td style="WORD-WRAP: break-word" bgcolor="#f3f3f3"><font size="2"><font style="FONT-WEIGHT: bold; COLOR: #990000">以下是引用片段：</font><br />&lt;?php<br />//获取搜索关键字<br />$keyword=trim($_POST[“keyword”]);<br />//检查是否为空<br />if($keyword==””)&#123;<br />   echo”您要搜索的关键字不能为空”;<br />   exit;//结束程序<br />&#125;<br />?&gt;<br /></font></td></tr></tbody></table></div><div><br /><font size="2">这样如果访问者输入的关键字为空时，可以做出提示。下面是遍历所有文件。<br /><br />我们可以用递归的方法遍历所有的文件，可以用函数opendir,readdir,也可以用PHP Directory的类。我们现在用前者.<br /></font></div><div><table style="BORDER-RIGHT: #cccccc 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: #cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellspacing="0" cellpadding="6" width="95%" align="center" border="0"><tbody><tr><td style="WORD-WRAP: break-word" bgcolor="#f3f3f3"><font size="2"><font style="FONT-WEIGHT: bold; COLOR: #990000">以下是引用片段：</font><br />&lt;?php<br />  //遍历所有文件的函数<br />  function listFiles($dir)&#123;<br />   $handle=opendir($dir);<br />   while(false!==($file=readdir($handle)))&#123;<br />          if($file!=&quot;.&quot;&amp;&amp;$file!=&quot;..&quot;)&#123;<br />          //如果是目录就继续搜索<br />          if(is_dir(&quot;$dir/$file&quot;))&#123;<br />             listFiles(&quot;$dir/$file&quot;);<br />          &#125;<br />              else&#123;<br />            <strong><font color="#ff0000">//在这里进行处理</font></strong><br />             &#125;<br />      &#125;<br />   &#125;<br />&#125;<br /><br />?&gt;</font></td></tr></tbody></table></div><div><br /><font size="2">在红字的地方我们可以对搜索到的文件进行读取，处理.下面就是读取文件内容，并检查内容中是否含有关键字$keyword,如果含有就把文件地址赋给一个数组。<br /></font></div><div><table style="BORDER-RIGHT: #cccccc 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: #cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellspacing="0" cellpadding="6" width="95%" align="center" border="0"><tbody><tr><td style="WORD-WRAP: break-word" bgcolor="#f3f3f3"><font size="2"><font style="FONT-WEIGHT: bold; COLOR: #990000">以下是引用片段：</font><br />&lt;?php<br />//$dir是搜索的目录,$keyword是搜索的关键字 ,$array是存放的数组<br />function listFiles($dir,$keyword,&amp;$array)&#123;<br />   $handle=opendir($dir);<br />   while(false!==($file=readdir($handle)))&#123;<br />          if($file!=&quot;.&quot;&amp;&amp;$file!=&quot;..&quot;)&#123;<br />          if(is_dir(&quot;$dir/$file&quot;))&#123;<br />             listFiles(&quot;$dir/$file&quot;,$keyword,$array);<br />          &#125;<br />              else&#123;<br />            //读取文件内容<br />            $data=fread(fopen(&quot;$dir/$file&quot;,&quot;r&quot;),filesize(&quot;$dir/$file&quot;));<br />            //不搜索自身<br />            if($file!=”search.php”)&#123;<br />              //是否匹配<br />                          if(eregi(&quot;$keyword&quot;,$data))&#123;<br />                  $array[]=&quot;$dir/$file&quot;;<br />                          &#125;<br />            &#125;<br />             &#125;<br />      &#125;<br />   &#125;<br />&#125;<br />//定义数组$array<br />$array=array();<br />//执行函数<br />listFiles(&quot;.&quot;,&quot;php&quot;,$array);<br />//打印搜索结果<br />foreach($array as $value)&#123;<br />   echo &quot;$value&quot;.&quot;&lt;br&gt;n&quot;;<br />&#125;<br />?&gt;<br /></font></td></tr></tbody></table></div><div><br /><font size="2">现在把这个结果和开头的一段程序结合起来，输入一个关键字，然后就会发现你的网站中的相关结果都被搜索出来了。我们现在在把它完善一下。<br />1,列出内容的标题<br />把<br /></font><table style="BORDER-RIGHT: #cccccc 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: #cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellspacing="0" cellpadding="6" width="95%" align="center" border="0"><tbody><tr><td style="WORD-WRAP: break-word" bgcolor="#f3f3f3"><font size="2"><font style="FONT-WEIGHT: bold; COLOR: #990000">以下是引用片段：</font><br />if(eregi(&quot;$keyword&quot;,$data))<br />&#123; </font><div><font size="2">    $array[]=&quot;$dir/$file&quot;;<br />&#125;</font></div></td></tr></tbody></table><br /><font size="2">改成<br /></font><table style="BORDER-RIGHT: #cccccc 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: #cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellspacing="0" cellpadding="6" width="95%" align="center" border="0"><tbody><tr><td style="WORD-WRAP: break-word" bgcolor="#f3f3f3"><font size="2"><font style="FONT-WEIGHT: bold; COLOR: #990000">以下是引用片段：</font><br /> if(eregi(&quot;$keyword&quot;,$data))&#123;<br />     if(eregi(&quot;&lt;title&gt;(.+)&lt;/title&gt;&quot;,$data,$m))&#123;<br />         $title=$m[&quot;1&quot;];<br />     &#125;<br />     else<br />    &#123;<br />          $title=&quot;没有标题&quot;;<br />     &#125;<br />       $array[]=&quot;$dir/$file $title&quot;;<br /> &#125;</font></td></tr></tbody></table><br /><font size="2">原理就是，如果在文件内容中找到&lt;title&gt;xxx&lt;/title&gt;，那么就把xxx取出来作为标题，如果找不到那么就把标题命名未”没有标题”.<br /><br />2,只搜索网页的内容的主题部分。<br />做网页时一定会有很多html代码在里面，而这些都不是我们想要搜索的，所以要去除它们。我现在用正则表达式和strip_tags的配合,并不能把所有的都去掉。<br />把<br /></font><table style="BORDER-RIGHT: #cccccc 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: #cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellspacing="0" cellpadding="6" width="95%" align="center" border="0"><tbody><tr><td style="WORD-WRAP: break-word" bgcolor="#f3f3f3"><font size="2"><font style="FONT-WEIGHT: bold; COLOR: #990000">以下是引用片段：</font><br />$data=fread(fopen(&quot;$dir/$file&quot;,&quot;r&quot;),filesize(&quot;$dir/$file&quot;));<br /> //不搜索自身<br /> if($file!=”search.php”)&#123;<br />     //是否匹配<br />     if(eregi(&quot;$keyword&quot;,$data))&#123;</font></td></tr></tbody></table><br /><font size="2">改为 <br /></font><table style="BORDER-RIGHT: #cccccc 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: #cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellspacing="0" cellpadding="6" width="95%" align="center" border="0"><tbody><tr><td style="WORD-WRAP: break-word" bgcolor="#f3f3f3"><font size="2"><font style="FONT-WEIGHT: bold; COLOR: #990000">以下是引用片段：</font><br />$data=fread(fopen(&quot;$dir/$file&quot;,&quot;r&quot;),filesize(&quot;$dir/$file&quot;));<br />if(eregi(&quot;&lt;body([^&gt;]+)&gt;(.+)&lt;/body&gt;&quot;,$data,$b))&#123;<br />    $body=strip_tags($b[&quot;2&quot;]);<br />&#125;<br /> else&#123;<br />     $body=strip_tags($data);<br /> &#125;<br /> if($file!=&quot;search.php&quot;)&#123;<br />     if(eregi(&quot;$keyword&quot;,$body))&#123;<br /></font></td></tr></tbody></table><br /><font size="2">3,标题上加链接<br /></font><table style="BORDER-RIGHT: #cccccc 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: #cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellspacing="0" cellpadding="6" width="95%" align="center" border="0"><tbody><tr><td style="WORD-WRAP: break-word" bgcolor="#f3f3f3"><font size="2"><font style="FONT-WEIGHT: bold; COLOR: #990000">以下是引用片段：</font><br />foreach($array as $value)&#123;<br />   echo &quot;$value&quot;.&quot;&lt;br&gt;n&quot;;<br />&#125;<br /></font></td></tr></tbody></table><font size="2">改成<br /></font><table style="BORDER-RIGHT: #cccccc 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: #cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellspacing="0" cellpadding="6" width="95%" align="center" border="0"><tbody><tr><td style="WORD-WRAP: break-word" bgcolor="#f3f3f3"><font size="2"><font style="FONT-WEIGHT: bold; COLOR: #990000">以下是引用片段：</font><br />foreach($array as $value)&#123;<br />   //拆开<br />   list($filedir,$title)=split(“[ ]”,$value,”2”);<br />   //输出<br />   echo &quot;&lt;a href=$filedir&gt;$value&lt;/a&gt;&quot;.&quot;&lt;br&gt;n&quot;;<br />&#125;</font></td></tr></tbody></table><br /><font size="2">4防止超时<br />如果文件比较多，那么防止PHP执行时间超时是必要的。可以在文件头加上<br />set_time_limit(“600”);<br />以秒为单位，所以上面是设10分钟为限。<br /><br /><br />所以完整的程序就是<br /></font><table style="BORDER-RIGHT: #cccccc 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: #cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellspacing="0" cellpadding="6" width="95%" align="center" border="0"><tbody><tr><td style="WORD-WRAP: break-word" bgcolor="#f3f3f3"><font size="2"><font style="FONT-WEIGHT: bold; COLOR: #990000">以下是引用片段：</font><br />&lt;?php<br />set_time_limit(&quot;600&quot;);<br />//获取搜索关键字<br />$keyword=trim($_POST[&quot;keyword&quot;]);<br />//检查是否为空<br />if($keyword==&quot;&quot;)&#123;<br />   echo&quot;您要搜索的关键字不能为空&quot;;<br />   exit;//结束程序<br />&#125;<br />function listFiles($dir,$keyword,&amp;$array)&#123;<br />   $handle=opendir($dir);<br />   while(false!==($file=readdir($handle)))&#123;<br />          if($file!=&quot;.&quot;&amp;&amp;$file!=&quot;..&quot;)&#123;<br />          if(is_dir(&quot;$dir/$file&quot;))&#123;<br />             listFiles(&quot;$dir/$file&quot;,$keyword,$array);<br />          &#125;<br />              else&#123;<br />            $data=fread(fopen(&quot;$dir/$file&quot;,&quot;r&quot;),filesize(&quot;$dir/$file&quot;));<br />                        if(eregi(&quot;&lt;body([^&gt;]+)&gt;(.+)&lt;/body&gt;&quot;,$data,$b))&#123;<br />                 $body=strip_tags($b[&quot;2&quot;]);<br />                        &#125;<br />                        else&#123;<br />                 $body=strip_tags($data);<br />                        &#125;<br />                        if($file!=&quot;search.php&quot;)&#123;<br />                            if(eregi(&quot;$keyword&quot;,$body))&#123;<br />                                   if(eregi(&quot;&lt;title&gt;(.+)&lt;/title&gt;&quot;,$data,$m))&#123;<br />                        $title=$m[&quot;1&quot;];<br />                                   &#125;<br />                                   else&#123;<br />                        $title=&quot;没有标题&quot;;<br />                                   &#125;<br />                                   $array[]=&quot;$dir/$file $title&quot;;<br />                            &#125;<br />                        &#125;<br />             &#125;<br />      &#125;<br />   &#125;<br />&#125;<br />$array=array();<br />listFiles(&quot;.&quot;,&quot;$keyword&quot;,$array);<br />foreach($array as $value)&#123;<br />   //拆开<br />   list($filedir,$title)=split(&quot;[ ]&quot;,$value,&quot;2&quot;);<br />   //输出<br />   echo &quot;&lt;a href=$filedir target=_blank&gt;$title &lt;/a&gt;&quot;.&quot;&lt;br&gt;n&quot;;<br />&#125;<br />?&gt;<br /></font></td></tr></tbody></table><br /><font size="2">到此为止，你已经做好了自己的一个搜索引擎，你也可以通过修改内容处理部分来改进它，可以实现搜索标题，或者搜索内容的功能。也可以考虑分页。这些都留给你自己吧。<br /><br />这里说明一下用preg_match代替eregi，会快很多。这里只是为了通俗易懂，所以使用了常用的eregi.</font></div>
]]>
</description>
</item><item>
<link>https://atim.cn/post//#blogcomment</link>
<title><![CDATA[[评论] 做个自己站内搜索引擎]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>https://atim.cn/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>