Heim  >  Artikel  >  Backend-Entwicklung  >  探讨PHP实现站内搜索引擎的具体方法_PHP教程

探讨PHP实现站内搜索引擎的具体方法_PHP教程

WBOY
WBOYOriginal
2016-07-15 13:29:441097Durchsuche

我们在实际的编码中,可以利用以下就是PHP实现站内搜索引擎的具体代码:

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><strong><font color="#006699"></font></strong></span><span>   </span></span></li>
<li class="">
<span></span><span class="tag-name"><strong><font color="#006699">include</font></strong></span><span>("include/common.inc");   </span>
</li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">title</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">"Search"</font></span><span>;   </span>
</li>
<li class=""><span>include("include/header.inc");   </span></li>
<li class="alt">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span>   </span>
</li>
<li class="">
<span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">P</span><span class="tag">></span></span></font></strong><span>   </span>
</li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">FORM</span></span></font></strong><span> </span><span class="attribute"><font color="#ff0000">ACTION</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">" echo "</font></span><span>$PHP_SELF"; </span><span class="tag"><strong><font color="#006699">?></font></strong></span><span>" </span><span class="attribute"><font color="#ff0000">METHOD</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"POST"</font></span><span class="tag"><strong><font color="#006699">></font></strong></span><span>   </span>
</li>
<li class="">
<span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">INPUT</span></span></font></strong><span> </span><span class="attribute"><font color="#ff0000">TYPE</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"text"</font></span><span> </span><span class="attribute"><font color="#ff0000">NAME</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"searchstr"</font></span><span> </span><span class="attribute"><font color="#ff0000">value</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">" echo "</font></span><span>$searchstr"; </span><span class="tag"><strong><font color="#006699">?></font></strong></span><span>"   </span>
</li>
<li class="alt">
<span></span><span class="attribute"><font color="#ff0000">SIZE</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"20"</font></span><span> </span><span class="attribute"><font color="#ff0000">MAXLENGTH</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"30"</font></span><span class="tag"><strong><font color="#006699">></font></strong></span><span>   </span>
</li>
<li class="">
<span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">INPUT</span></span></font></strong><span> </span><span class="attribute"><font color="#ff0000">TYPE</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"submit"</font></span><span> </span><span class="attribute"><font color="#ff0000">VALUE</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"Search!"</font></span><span class="tag"><strong><font color="#006699">></font></strong></span><span>   </span>
</li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">FORM</span><span class="tag">></span></font></strong><span>   </span>
</li>
<li class="">
<span></span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">P</span><span class="tag">></span></font></strong><span>   </span>
</li>
<li class="alt">
<span></span><span class="tag"><strong><font color="#006699"></font></strong></span><span>   </span>
</li>
<li class="">
<span></span><span class="tag-name"><strong><font color="#006699">if</font></strong></span><span> ( ! empty($searchstr) )   </span>
</li>
<li class="alt"><span>{   </span></li>
<li class=""><span>// empty()用来检查查询字串是否为空   </span></li>
<li class="alt"><span>// 如果不为空,调用grep查询   </span></li>
<li class="">
<span>echo "</span><strong><font color="#006699"><span class="tag"><span class="tag-name">HR</span><span class="tag">></span></span></font></strong><span>n";   </span>
</li>
<li class="alt"><span>// 调用grep对所有文件进行大小写非敏感模式的查询   </span></li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">cmdstr</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">"grep -i $searchstr *"</font></span><span>;   </span>
</li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">fp</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">popen</font></span><span>( $cmdstr, "r" ); // 执行命令并输出管道   </span>
</li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">myresult</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array</font></span><span>(); // 存储查询结果   </span>
</li>
<li class="alt">
<span>while( $</span><span class="attribute"><font color="#ff0000">buffer</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">fgetss</font></span><span> ($fp, 4096))   </span>
</li>
<li class=""><span>{   </span></li>
<li class="alt"><span>// grep返回这样格式: 文件名:匹配字串出现行数   </span></li>
<li class=""><span>// 因此我们利用函数split()分离处理数据   </span></li>
<li class="alt"><span>list($fname, $fline) = split(":",$buffer, 2);   </span></li>
<li class=""><span>// 我们只输出第一次匹配的结果   </span></li>
<li class="alt"><span>if ( !defined($myresult[$fname]))   </span></li>
<li class=""><span>$myresult[$fname] = $fline;   </span></li>
<li class="alt"><span>}   </span></li>
<li class=""><span>// 现在我们将结果存储在数组中,下面就可以处理并输出了   </span></li>
<li class="alt"><span>if ( count($myresult) )   </span></li>
<li class=""><span>{   </span></li>
<li class="alt">
<span>echo "</span><strong><font color="#006699"><span class="tag"><span class="tag-name">OL</span><span class="tag">></span></span></font></strong><span>n";   </span>
</li>
<li class=""><span>while(list($fname,$fline) = each($myresult))   </span></li>
<li class="alt">
<span>echo "</span><strong><font color="#006699"><span class="tag"><span class="tag-name">LI</span><span class="tag">></span></span></font></strong><span>   </span>
</li>
<li class="">
<span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">A</span></span></font></strong><span> </span><span class="attribute"><font color="#ff0000">HREF</font></span><span>="$fname"</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>$fname</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">A</span><span class="tag">></span></font></strong><span> : $fline </span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">LI</span><span class="tag">></span></font></strong><span>n";   </span>
</li>
<li class="alt">
<span>echo "</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">OL</span><span class="tag">></span></font></strong><span>n";   </span>
</li>
<li class=""><span>}   </span></li>
<li class="alt"><span>else   </span></li>
<li class=""><span>{   </span></li>
<li class="alt"><span>// 如果没有查询结果   </span></li>
<li class="">
<span>echo "Sorry. Search on </span><strong><font color="#006699"><span class="tag"><span class="tag-name">B</span><span class="tag">></span></span></font></strong><span>$searchstr</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">B</span><span class="tag">></span></font></strong><span>   </span>
</li>
<li class="alt">
<span>returned no results.</span><strong><font color="#006699"><span class="tag"><span class="tag-name">BR</span><span class="tag">></span></span></font></strong><span>n";   </span>
</li>
<li class=""><span>}   </span></li>
<li class="alt"><span>pclose($fp);   </span></li>
<li class=""><span>}   </span></li>
<li class="alt">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span>   </span>
</li>
<li class="">
<span></span><span class="tag"><strong><font color="#006699"></font></strong></span><span>   </span>
</li>
<li class="alt">
<span></span><span class="tag-name"><strong><font color="#006699">include</font></strong></span><span>("include/footer.inc");   </span>
</li>
<li class="">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span>  </span>
</li>
</ol>

怎么样,通过对上面这段代码的理解,新手们是否已经掌握了PHP实现站内搜索引擎的具体方法呢?


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446340.htmlTechArticle我们在实际的编码中,可以利用 以下就是PHP实现站内搜索引擎的具体代码: ? include ("include/common.inc"); $ title = "Search" ; include("include/header....
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn