Home > Backend Development > PHP Tutorial > How to brighten the search keywords in the php site, search keywords in the php site_PHP tutorial

How to brighten the search keywords in the php site, search keywords in the php site_PHP tutorial

WBOY
Release: 2016-07-13 10:10:30
Original
866 people have browsed it

How to brighten the search keywords in the php site, search keywords in the php site

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 them with highlighted words that are the same as the search keywords. 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:

Copy code 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:
Copy code 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:
Copy code 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/935488.htmlTechArticleHow to brighten the search keywords in the php site, search keywords in the php site. This article explains the search key in the php site with an example How to make words brighter. Share it with everyone for your reference. Specific points...
Related labels:
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