search
Homephp教程php手册看PHP如何实现多关键字加亮

  项目结构:

 

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

  搜索结果: 高亮显示

  项目所需数据库结构:

  实现代码:

  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] ?> 
        
编辑                  href="delete.php?id==$row[id]?>">删除
 
          
      
      
         内容:=$row[content]?> 
      
      
          
        
发表日期:=$row[lastdate]?>
 
          
      
          } 
     ?> 
 

 

  说明:在这个小程序中,有一点不足之处在于,只能同时搜索两个关键字,并且中间用空格" "隔开,如果只是搜索一个关键字,如:"大"

  显示的时候会出现乱码 ……^_^,这是由于下面代码的结果:

  //用空格符把关键字分割开

  $key=explode(' ', $_GET[keyWord]);

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



Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MantisBT

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

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows

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

SublimeText3 Chinese version

Chinese version, very easy to use