Home > Backend Development > PHP Tutorial > PHP multiple keywords highlight implementation code_PHP tutorial

PHP multiple keywords highlight implementation code_PHP tutorial

WBOY
Release: 2016-07-21 15:19:07
Original
781 people have browsed it

Project structure:

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

Search results: Highlight

Database structure required for the project:


Implementation code:
conn.php

Copy code The code is as follows:

$conn = @ mysql_connect("localhost", "root", "") or die("Database link error");
mysql_select_db("form", $conn);
mysql_query("set names 'gbk'");
?>

searchAndDisplayWithColor.php
Copy code Code As follows:

include 'conn.php';
?>







Keyword:


cellspacing="1" bgcolor="#add3ef">
//Only perform related searches when the keyword is not empty
if($_GET['keyWord']){
//Separate the keywords with spaces
$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)){
//Replace keyword, And highlight the keyword
$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]);
?>





< td>Content:




}
?>
Title: User:


Publication date:



Note: In this small program, there is one shortcoming, only You can search for two keywords at the same time, separated by a space " ". If you only search for one keyword, such as: "big"
, garbled characters will appear when displayed... ^|_|^, this is due to the following The result of the code:
Copy the code The code is as follows:

//Separate the keywords with spaces
$ key=explode(' ', $_GET[keyWord]);

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325331.htmlTechArticleProject structure: Start search: Search keywords here ("big" "this") Search results: Highlighted Database structure required for the project: Implementation code: conn.php Copy the code as follows: ?p...
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