看PHP如何实现多关键字加亮

开发 后端
在这个小程序中,有一点不足之处在于,只能同时搜索两个关键字,并且中间用空格" "隔开。但也并非不可改善。

项目结构:

开始搜索:   这里搜索关键字("大""这")

搜索结果:  高亮显示

项目所需数据库结构:

实现代码:

conn.php

  1. <?php  
  2. $conn = @ mysql_connect("localhost""root"""or die("数据库链接错误");  
  3. mysql_select_db("form"$conn);  
  4. mysql_query("set names 'gbk'");   
  5. ?> 

searchAndDisplayWithColor.php

 
 
  1. <?php  
  2. include 'conn.php';  
  3. ?>  
  4. <table width=500 align="center">  
  5.      <form action="" method="get">  
  6.      <tr>  
  7.          <td>关键字:<input type="text" name="keyWord" />  
  8.          <input type="submit" value="搜索" /></td>  
  9.      </tr>  
  10.      </form>  
  11.  </table>  
  12.    
  13.  <table width=500 border="0" align="center" cellpadding="5" 
  14.      cellspacing="1" bgcolor="#add3ef">  
  15.      <?php  
  16.      //关键字不为空的时候才执行相关搜索  
  17.      if($_GET['keyWord']){  
  18.      //用空格符把关键字分割开  
  19.      $key=explode(' '$_GET[keyWord]);  
  20.      $sql="select * from message where title like '$key[0]' or title like '$key[1]' or content like '$key[0]' or content like '%$key[1]%'";  
  21.      $query=mysql_query($sql);  
  22.      while ($row=mysql_fetch_array($query)){  
  23.          //替换关键字,并且把关键字高亮显示  
  24.          $row[title]=preg_replace("/$key[0]/i""<font color=red><b>$key[0]</b></font>"$row[title]);  
  25.          $row[title]=preg_replace("/$key[0]/i""<font color=red><b>$key[1]</b></font>"$row[title]);  
  26.          $row[content]=preg_replace("/$key[0]/i""<font color=red><b>$key[0]</b></font>"$row[content]);  
  27.          $row[content]=preg_replace("/$key[1]/i""<font color=red><b>$key[1]</b></font>"$row[content]);  
  28.          ?>  
  29.    
  30.      <tr bgcolor="#eff3ff">  
  31.          <td>标题:<font color="black"><?=$row[title]?></font> 用户:<font color="black"><?=$row[user] ?></font>  
  32.          <div align="right"><a href="preEdit.php?id=<?=$row[id]?>">编辑</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a  
  33.              href="delete.php?id=<?=$row[id]?>">删除</a></div>  
  34.          </td>  
  35.      </tr>  
  36.      <tr bgColor="#ffffff">  
  37.          <td>内容:<?=$row[content]?></td>  
  38.      </tr>  
  39.      <tr bgColor="#ffffff">  
  40.          <td>  
  41.          <div align="right">发表日期:<?=$row[lastdate]?></div>  
  42.          </td>  
  43.      </tr>  
  44.      <?php }  
  45.      }  
  46.      ?>  
  47.  </table> 

说明:在这个小程序中,有一点不足之处在于,只能同时搜索两个关键字,并且中间用空格" "隔开,如果只是搜索一个关键字,如:"大"
显示的时候会出现乱码 ……^|_|^,这是由于下面代码的结果:

  1.  //用空格符把关键字分割开  
  2.  $key=explode(' '$_GET[keyWord]); 

如果要改进的话,在这里的后面就要做一下判断了。

 

责任编辑:张伟 来源: Hongten的博客
相关推荐

2009-11-26 19:24:54

PHP类CMS

2009-12-10 13:23:29

PHP关键字this

2010-10-08 15:37:21

MySQL单表

2009-12-08 18:02:06

PHP final关键

2009-12-10 13:31:20

PHP self关键字

2009-11-30 10:23:35

fixed关键字

2009-12-03 19:06:44

PHP关键字globa

2022-01-04 16:35:42

C++Protected关键字

2009-09-17 09:30:00

Linq LET关键字

2009-09-02 09:24:03

C# this关键字

2012-03-01 12:50:03

Java

2009-08-21 14:58:56

C# this关键字

2018-04-20 15:56:09

Pythonglobal关键字

2013-01-30 10:12:14

Pythonyield

2010-02-06 10:09:47

C++模拟event关

2022-05-06 08:32:40

Pythonwith代码

2021-02-01 13:10:07

Staticc语言UNIX系统

2009-08-13 13:04:29

C# lock关键字

2022-06-29 08:05:25

Volatile关键字类型

2022-02-17 08:31:38

C语言staic关键字
点赞
收藏

51CTO技术栈公众号