看PHP如何实现多关键字加亮_PHP教程
项目结构:
开始搜索: 这里搜索关键字("大""这")
搜索结果: 高亮显示
项目所需数据库结构:
实现代码:
conn.php
<ol class="dp-c"> <li class="alt"><span><span><?php </span></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></li> </ol>
searchAndDisplayWithColor.php
<ol class="dp-c"> <li class="alt"><span><span><?php </span></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></li> <li><span><table> <span class="string">"center"</span><span>> </span><li class="alt"><span> <form> <span class="string">""</span><span> method=</span><span class="string">"get"</span><span>> </span> </form></span></li> <li><span> <tr> <li class="alt"> <span> </span><td>关键字:<input><span class="string">"text"</span><span> name=</span><span class="string">"keyWord"</span><span> /> </span><li><span> <input><span class="string">"submit"</span><span> value=</span><span class="string">"搜索"</span><span> /></span></span></li> </td> </li> <li class="alt"><span> </span></li> </tr> </span></li> <li><span> </span></li> <li class="alt"><span> </span></li> </table> </span></li> <li><span> </span></li> <li class="alt"><span> <table> <span class="string">"0"</span><span> align=</span><span class="string">"center"</span><span> cellpadding=</span><span class="string">"5"</span><span> </span><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></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><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><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><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><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> <span class="string">"#eff3ff"</span><span>> </span><li class="alt"> <span> </span><td>标题:<font><span class="string">"black"</span><span>>=</span><span class="vars">$row</span><span>[title]?></span></font> 用户:<font><span class="string">"black"</span><span>>=</span><span class="vars">$row</span><span>[user] ?></span></font> <li><span> <div> <span class="string">"right"</span><span>><a><span class="string">"preEdit.php?id==$row[id]?>"</span><span>>编辑</span></a> | <a><li class="alt"> <span> href=</span><span class="string">"delete.php?id==$row[id]?>"</span><span>>删除</span> </li></a></span> </div> </span></li> <li><span> </span></li> </td> </li> <li class="alt"><span> </span></li> </tr> </span></li> <li><span> <tr> <span class="string">"#ffffff"</span><span>> </span><li class="alt"> <span> </span><td>内容:=<span class="vars">$row</span><span>[content]?></span> </td> </li> <li><span> </span></li> </tr> </span></li> <li class="alt"><span> <tr> <span class="string">"#ffffff"</span><span>> </span><li> <span> </span><td> <li class="alt"><span> <div> <span class="string">"right"</span><span>>发表日期:=</span><span class="vars">$row</span><span>[lastdate]?></span> </div> </span></li> <li><span> </span></li> </td> </li> <li class="alt"><span> </span></li> </tr> </span></li> <li><span> <?php } </span></span></li> <li class="alt"><span> } </span></li> <li><span> ?> </span></li> <li class="alt"><span> </span></li> </table> </span></li> </ol>
说明:在这个小程序中,有一点不足之处在于,只能同时搜索两个关键字,并且中间用空格" "隔开,如果只是搜索一个关键字,如:"大"
显示的时候会出现乱码 ……^|_|^,这是由于下面代码的结果:
<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>
如果要改进的话,在这里的后面就要做一下判断了。

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

PHP 8.4 带来了多项新功能、安全性改进和性能改进,同时弃用和删除了大量功能。 本指南介绍了如何在 Ubuntu、Debian 或其衍生版本上安装 PHP 8.4 或升级到 PHP 8.4

CakePHP 是 PHP 的开源框架。它的目的是使应用程序的开发、部署和维护变得更加容易。 CakePHP 基于类似 MVC 的架构,功能强大且易于掌握。模型、视图和控制器 gu

登录 CakePHP 是一项非常简单的任务。您只需使用一项功能即可。您可以记录任何后台进程(如 cronjob)的错误、异常、用户活动、用户采取的操作。在 CakePHP 中记录数据很容易。提供了 log() 函数

Visual Studio Code,也称为 VS Code,是一个免费的源代码编辑器 - 或集成开发环境 (IDE) - 可用于所有主要操作系统。 VS Code 拥有针对多种编程语言的大量扩展,可以轻松编写

CakePHP 是一个开源MVC 框架。它使开发、部署和维护应用程序变得更加容易。 CakePHP 有许多库可以减少大多数常见任务的过载。
