Home  >  Article  >  Backend Development  >  See how to implement multiple keyword highlighting in PHP_PHP Tutorial

See how to implement multiple keyword highlighting in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-20 10:58:13759browse

Project structure:

Start the search: Search keywords here ("big""this" )

Search results: Highlight

Database structure required for the project:

Implementation code:

conn.php

<ol class="dp-c"><li class="alt"><span><span><?php  </span></span></li><li class="alt"><span class="vars">$conn</span><span> = @ mysql_connect(</span><span class="string">"localhost"</span><span>, </span><span class="string">"root"</span><span>, </span><span class="string">""</span><span>) </span><span class="keyword">or</span><span> </span><span class="keyword">die</span><span>(</span><span class="string">"数据库链接错误"</span><span>);  </span></li><li><span>mysql_select_db(</span><span class="string">"form"</span><span>, </span><span class="vars">$conn</span><span>);  </span></li><li class="alt"><span>mysql_query(</span><span class="string">"set names 'gbk'"</span><span>);   </span></li><li><span>?> </span></span></li></ol>

searchAndDisplayWithColor.php

<ol class="dp-c">
<li class="alt"><span><span><?php  </span></span></li><li><span class="keyword">include</span><span> </span><span class="string">'conn.php'</span><span>;  </span></li><li class="alt"><span>?>  </span></span></li>
<li><span><table width=500 align=</span><span class="string">"center"</span><span>>  </span></li>
<li class="alt"><span>     <form action=</span><span class="string">""</span><span> method=</span><span class="string">"get"</span><span>>  </span></li>
<li><span>     <tr>  </span></li>
<li class="alt"><span>         <td>关键字:<input type=</span><span class="string">"text"</span><span> name=</span><span class="string">"keyWord"</span><span> />  </span></li>
<li><span>         <input type=</span><span class="string">"submit"</span><span> value=</span><span class="string">"搜索"</span><span> /></td>  </span></li>
<li class="alt"><span>     </tr>  </span></li>
<li><span>     </form>  </span></li>
<li class="alt"><span> </table>  </span></li>
<li><span>   </span></li>
<li class="alt"><span> <table width=500 border=</span><span class="string">"0"</span><span> align=</span><span class="string">"center"</span><span> cellpadding=</span><span class="string">"5"</span><span> </span></li><li><span>     cellspacing=</span><span class="string">"1"</span><span> bgcolor=</span><span class="string">"#add3ef"</span><span>>  </span></li>
<li class="alt">
<span>     <?php  </span></li><li><span>     </span><span class="comment">//关键字不为空的时候才执行相关搜索 </span><span> </span></li><li class="alt"><span>     </span><span class="keyword">if</span><span>(</span><span class="vars">$_GET</span><span>[</span><span class="string">'keyWord'</span><span>]){  </span></li><li><span>     </span><span class="comment">//用空格符把关键字分割开 </span><span> </span></li><li class="alt"><span>     </span><span class="vars">$key</span><span>=</span><span class="func">explode</span><span>(</span><span class="string">' '</span><span>, </span><span class="vars">$_GET</span><span>[keyWord]);  </span></li><li><span>     </span><span class="vars">$sql</span><span>=</span><span class="string">"select * from message where title like '$key[0]' or title like '$key[1]' or content like '$key[0]' or content like '%$key[1]%'"</span><span>;  </span></li><li class="alt"><span>     </span><span class="vars">$query</span><span>=mysql_query(</span><span class="vars">$sql</span><span>);  </span></li><li><span>     </span><span class="keyword">while</span><span> (</span><span class="vars">$row</span><span>=mysql_fetch_array(</span><span class="vars">$query</span><span>)){  </span></li><li class="alt"><span>         </span><span class="comment">//替换关键字,并且把关键字高亮显示 </span><span> </span></li><li><span>         </span><span class="vars">$row</span><span>[title]=preg_replace(</span><span class="string">"/$key[0]/i"</span><span>, </span><span class="string">"<font color=red><b>$key[0]</b></font>"</span><span>, </span><span class="vars">$row</span><span>[title]);  </span>
</li>
<li class="alt">
<span>         </span><span class="vars">$row</span><span>[title]=preg_replace(</span><span class="string">"/$key[0]/i"</span><span>, </span><span class="string">"<font color=red><b>$key[1]</b></font>"</span><span>, </span><span class="vars">$row</span><span>[title]);  </span>
</li>
<li>
<span>         </span><span class="vars">$row</span><span>[content]=preg_replace(</span><span class="string">"/$key[0]/i"</span><span>, </span><span class="string">"<font color=red><b>$key[0]</b></font>"</span><span>, </span><span class="vars">$row</span><span>[content]);  </span>
</li>
<li class="alt">
<span>         </span><span class="vars">$row</span><span>[content]=preg_replace(</span><span class="string">"/$key[1]/i"</span><span>, </span><span class="string">"<font color=red><b>$key[1]</b></font>"</span><span>, </span><span class="vars">$row</span><span>[content]);  </span>
</li>
<li><span>         ?>  </span></li>
<li class="alt"><span>   </span></li>
<li><span>     <tr bgcolor=</span><span class="string">"#eff3ff"</span><span>>  </span></li>
<li class="alt"><span>         <td>标题:<font color=</span><span class="string">"black"</span><span>><?=</span><span class="vars">$row</span><span>[title]?></font> 用户:<font color=</span><span class="string">"black"</span><span>><?=</span><span class="vars">$row</span><span>[user] ?></font>  </span></li>
<li>
<span>         <div align=</span><span class="string">"right"</span><span>><a href=</span><span class="string">"preEdit.php?id=<?=$row[id]?>"</span><span>>编辑</a>  |  <a  </span></li><li class="alt"><span>             href=</span><span class="string">"delete.php?id=<?=$row[id]?>"</span><span>>删除</a></div>  </span>
</li>
<li><span>         </td>  </span></li>
<li class="alt"><span>     </tr>  </span></li>
<li><span>     <tr bgColor=</span><span class="string">"#ffffff"</span><span>>  </span></li>
<li class="alt"><span>         <td>内容:<?=</span><span class="vars">$row</span><span>[content]?></td>  </span></li>
<li><span>     </tr>  </span></li>
<li class="alt"><span>     <tr bgColor=</span><span class="string">"#ffffff"</span><span>>  </span></li>
<li><span>         <td>  </span></li>
<li class="alt"><span>         <div align=</span><span class="string">"right"</span><span>>发表日期:<?=</span><span class="vars">$row</span><span>[lastdate]?></div>  </span></li>
<li><span>         </td>  </span></li>
<li class="alt"><span>     </tr>  </span></li>
<li><span>     <?php }  </span></li><li class="alt"><span>     }  </span></li><li><span>     ?>  </span></li>
<li class="alt"><span> </table> </span></li>
</ol>

Explanation: In this small program, there is one shortcoming The point is that you can only search for two keywords at the same time, and they are separated by a space " ". If you only search for one keyword, such as: "big"
, garbled characters will appear when displayed... ^|_|^ , This is due to the result of the following code:

<ol class="dp-c">
<li class="alt"><span><span> </span><span class="comment">//用空格符把关键字分割开 </span><span> </span></span></li>
<li>
<span> </span><span class="vars">$key</span><span>=</span><span class="func">explode</span><span>(</span><span class="string">' '</span><span>, </span><span class="vars">$_GET</span><span>[keyWord]); </span>
</li>
</ol>

If you want to improve, you will have to make a judgment later here.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445701.htmlTechArticleProject structure: 498)this.width=498;' onmousewheel = 'javascript: return big(this)' height ="183" alt="" width="274" src="http://www.bkjia.com/uploadfile/2013/0904/2013090409540695...
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