Home  >  Article  >  Backend Development  >  Discuss the specific methods of implementing on-site search engines in PHP_PHP Tutorial

Discuss the specific methods of implementing on-site search engines in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-15 13:29:441101browse

In actual coding, we can use The following is the specific code to implement the on-site search engine in PHP:

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><strong><font color="#006699"><?</FONT></STRONG></SPAN><SPAN>   </SPAN></SPAN><LI class=""><SPAN></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>include</FONT></STRONG></SPAN><SPAN>("include/common.inc");   </SPAN></SPAN><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></SPAN><LI class=""><SPAN>include("include/header.inc");   </SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></font></strong></span><span>   </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><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>FORM</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>?></span></font></strong><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><SPAN class=tag-name>INPUT</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>?></span></font></strong><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><SPAN class=tag-name>INPUT</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>></span></font></strong><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></SPAN><LI class=""><SPAN></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>if</FONT></STRONG></SPAN><SPAN> ( ! empty($searchstr) )   </SPAN></SPAN><LI class=alt><SPAN>{   </SPAN><LI class=""><SPAN>// empty()用来检查查询字串是否为空   </SPAN><LI class=alt><SPAN>// 如果不为空,调用grep查询   </SPAN><LI class=""><SPAN>echo "</SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>HR</SPAN><SPAN class=tag>></font></strong></span><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><SPAN class=tag-name>OL</SPAN><SPAN class=tag>></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><SPAN class=tag-name>LI</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>A</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>HREF</FONT></SPAN><SPAN>="$fname"</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong><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><SPAN class=tag-name>B</SPAN><SPAN class=tag>></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><SPAN class=tag-name>BR</SPAN><SPAN class=tag>></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></SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>include</FONT></STRONG></SPAN><SPAN>("include/footer.inc");   </SPAN></SPAN><LI class=""><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></font></strong></span><span>  </span>
</li>
</ol>

How about, through the understanding of the above code, whether the novices have mastered PHP to implement the on-site search engine What is the specific method?


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446340.htmlTechArticleIn actual coding, we can use the following specific code to implement the on-site search engine in PHP: ? include (" include/common.inc"); $ title = "Search" ; include("include/header....
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn