> 백엔드 개발 > PHP 튜토리얼 > 探讨PHP实现站内搜索引擎的具体方法_PHP教程

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

WBOY
풀어 주다: 2016-07-15 13:29:44
원래의
1130명이 탐색했습니다.

我们在实际的编码中,可以利用以下就是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....
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿