Home > Backend Development > PHP Tutorial > PHP implements search for multiple keywords and highlights program code_PHP tutorial

PHP implements search for multiple keywords and highlights program code_PHP tutorial

WBOY
Release: 2016-07-13 16:55:59
Original
995 people have browsed it

Keyword highlighting is relatively simple to implement in PHP. It only needs to obtain and use the extracted keywords for word segmentation and then use the str_replace() function to implement the replacement. The principle is relatively simple.

Project structure:

Start the search: Search keywords here ("big", "this")

Start the search: Search keywords here ("big", "this")

Search results: Highlight

Database structure required for the project:

Database connection file

 代码如下 复制代码
$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]);
         ?>
 
    
        
    
    
        
    
    
        
    
     }
?>
 
标题: 用户:
        
编辑 | href="delete.php?id=">删除

        
内容:

        
发表日期:

        

 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]);
         ?>
 
    
        
    
    
        
    
    
        
    
          }
     ?>
 
标题: 用户:
        
编辑  |                href="delete.php?id=">删除

        
内容:

        
发表日期:

        

Note: In this small program, one shortcoming is that you can only search for two keywords at the same time, and they are separated by a space " ". If you only search for one keyword, such as: "big"
Garbled characters will appear when displayed...^|_|^, which is the result of the following code:

Separate keywords with spaces

http://www.bKjia.c0m/phper/php-gj/39302.htm
The code is as follows
 代码如下 复制代码
$key=explode(' ', $_GET[keyWord]);
Copy code

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

If you want to improve, you have to make a judgment later here.

Summary,

The above keyword highlighting is just a very simple user submission. We have str_replaced the keyword to highlight it. If you want to do better, you can use the dedecms word segmentation system to segment the words and then operate it. A lot.

For the implementation method of dedecms word segmentation program, please refer to

A Chinese word segmentation function written in PHP

http://www.bKjia.c0m/phper/24/c8b9ff7300e56c042014a655b0bd6e34.htm

PHP code for Chinese word segmentation

Simple Chinese word segmentation code made by php

http://www.bKjia.c0m/phper/php-gj/38506.htm

http://www.bkjia.com/PHPjc/631628.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/631628.htmlTechArticle
Keyword highlighting is relatively simple to implement in php. He only needs to obtain and use the extracted keywords for word segmentation Then use the str_replace() function to implement the replacement. The principle is relatively simple...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template