项目结构:
开始搜索: 这里搜索关键字("大""这")
搜索结果: 高亮显示
项目所需数据库结构:
实现代码:
conn.php
$conn = @ mysql_connect("localhost", "root", "") or die("数据库链接错误");
mysql_select_db("form", $conn);
mysql_query("set names 'gbk'");
?>
searchAndDisplayWithColor.php
include 'conn.php';
?>
cellspacing="1" bgcolor="#add3ef">
//关键字不为空的时候才执行相关搜索
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", "$key[0]", $row[title]);
$row[title]=preg_replace("/$key[0]/i", "$key[1]", $row[title]);
$row[content]=preg_replace("/$key[0]/i", "$key[0]", $row[content]);
$row[content]=preg_replace("/$key[1]/i", "$key[1]", $row[content]);
?>
标题:=$row[title]?> 用户:=$row[user] ?>
内容:=$row[content]?>
}
?>
说明:在这个小程序中,有一点不足之处在于,只能同时搜索两个关键字,并且中间用空格" "隔开,如果只是搜索一个关键字,如:"大"
显示的时候会出现乱码 ……^_^,这是由于下面代码的结果:
//用空格符把关键字分割开
$key=explode(' ', $_GET[keyWord]);
如果要改进的话,在这里的后面就要做一下判断了。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version
Chinese version, very easy to use