Home > php教程 > php手册 > php开发_多关键字,高亮显示,php高亮显示

php开发_多关键字,高亮显示,php高亮显示

WBOY
Release: 2016-06-13 08:43:26
Original
942 people have browsed it

php开发_多关键字,高亮显示,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 ?>
Copy after login

searchAndDisplayWithColor.php

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

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

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

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template