本文主要介绍的是php实现多关键字加亮功能,可以实现在搜索的时候进行高亮提醒,具体实现代码如下:
项目结构:
搜索结果: 高亮显示
项目所需数据库结构:
实现代码:
conn.php
?
|
1
2
3
4
5
|
<?php
$conn = @ mysql_connect("localhost", "root", "") or die("数据库链接错误");
mysql_select_db("form", $conn);
mysql_query("set names 'gbk'");
?>
|
searchanddisplaywithcolor.php
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
<?php
include 'conn.php';
?>
<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['keyword']){
//用空格符把关键字分割开
$key=explode(' ', $_get[keyword]);
$sql="select * from message where title like '$key[0]' or title like '$key[1]' or content like '$key[0]' or content like '%$key[1]%'";
$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>
<div align="right"><a href="preedit.php?id=<?=$row[id]?>">编辑</a>|<a
href="delete.php?id=<?=$row[id]?>">删除</a></div>
</td>
</tr>
<tr bgcolor="#ffffff">
<td>内容:<?=$row[content]?></td>
</tr>
<tr bgcolor="#ffffff">
<td>
<div align="right">发表日期:<?=$row[lastdate]?></div>
</td>
</tr>
<?php }
}
?>
</table>
|
说明:在这个小程序中,有一点不足之处在于,只能同时搜索两个关键字,并且中间用空格" "隔开,如果只是搜索一个关键字,如:"大"
显示的时候会出现乱码 ……^|_|^,这是由于下面代码的结果:
?
|
1
2
|
//用空格符把关键字分割开
$key=explode(' ', $_get[keyword]);
|
希望本文所述对你有所帮助,php实现多关键字加亮功能内容就给大家介绍到这里了。希望大家继续关注我们的网站!想要学习php可以继续关注本站。
相关文章
猜你喜欢
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
在ASP.NET 2.0中操作数据之三十一:使用DataList来一行显示多条记录
2025-05-29 77 -
springboot 高版本后继续使用log4j的完美解决方法
2025-05-27 40 -
2025-06-04 54
-
2025-06-04 88
-
2025-05-29 13
热门评论




