Home >Backend Development >PHP Tutorial >Detailed introduction and example code of implementing multi-keyword highlighting function in PHP

Detailed introduction and example code of implementing multi-keyword highlighting function in PHP

黄舟
黄舟Original
2017-02-27 09:51:381194browse

This article mainly introduces the multi-keyword highlighting function in PHP, which can be highlighted during search. The specific implementation code is as follows:

Project structure:

Detailed introduction and example code of implementing multi-keyword highlighting function in PHP

Search results: Highlight

Detailed introduction and example code of implementing multi-keyword highlighting function in PHP

Database structure required for the project:

Detailed introduction and example code of implementing multi-keyword highlighting function in PHP

Implementation Code:

conn.php

<?php 
$conn = @ mysql_connect("localhost", "root", "") or die("数据库链接错误"); 
mysql_select_db("form", $conn); 
mysql_query("set names &#39;gbk&#39;"); 
?>

searchAndDisplayWithColor.php

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

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

//用空格符把关键字分割开 
 $key=explode(&#39; &#39;, $_GET[keyWord]);

The above is a detailed introduction to the multi-keyword highlighting function in PHP and the content of the example code. For more related content, please pay attention to the PHP Chinese website (www .php.cn)!


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