Home > Backend Development > PHP Tutorial > How to brighten search keywords in PHP site_PHP tutorial

How to brighten search keywords in PHP site_PHP tutorial

WBOY
Release: 2016-07-13 09:59:28
Original
871 people have browsed it

How to brighten the search keywords in the php site

This article mainly introduces the implementation method of brightening the search keywords in the php site, and analyzes it in detail in the form of examples. The creation of database tables and the implementation skills of highlighting search keywords for the database are of great practical value. Friends in need can refer to it

The example in this article describes how to brighten search keywords in the PHP site. Share it with everyone for your reference. The specific analysis is as follows:

What we do is to take out the search results and replace the same search keywords with highlighted words. We will use str_replace (the keyword you are looking for, you are looking for Keywords, $str);

It’s that easy, let’s take a look at an example.

Create a database first: create database 'searchKey';

Create the table again, the SQL database creation code is as follows:

The code is as follows:

CREATE TABLE `fangke_cc`.`search` (
`id` INT( 4 ) NOT NULL AUTO_INCREMENT ,
`keyword` VARCHAR( 20 ) NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE = MYISAM
We import some data and the SQL database creation code is as follows:

The code is as follows:

INSERT INTO `search` (`id`, `keyword`) VALUES
(1, 'China's first stop for script programming www.jb51.net'),
(2, 'China's first stop for script programming www.jb51.net'),
(3, 'China's first stop for script programming www.jb51.net'),
(4, 'China's first stop for script programming www.jb51.net');
Okay, let’s perform the query operation. The example code is as follows:

The code is as follows:

if( $_POST) {
$db ='fangke_cc';
mysql_pconnect('localhost','root','root') or die(mysql_error());
mysql_select_db($db);
mysql_query("set names 'gbk'");
$key = $_POST['keyword'];
$sql = "Select * from search where keyword like '%$key%'";
$query = mysql_query($sql);
while( $rs = mysql_fetch_array( $query ) )
{
echo str_replace($key,"$key",$rs['keyword']),'
';
}
}

?>



I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/975879.htmlTechArticleHow to brighten the search keywords in the php site. This article mainly introduces how to brighten the search keywords in the php site. Implementation method, a more detailed analysis of the creation and creation of database tables in the form of examples...
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