Home > Article > Backend Development > How to brighten search keywords on the site_PHP tutorial
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', "the keyword you are looking for带有高亮的html标签>", $str);
How to highlight search keywords on the site
What we do is to take out the search results and replace the same search keywords with highlighted words.
We will use str_replace('you The keyword you are looking for', "The keyword you are looking for", $str);
It’s that easy, okay Let’s look at an example below.
First create a database tutorial create database 'searchKey';
Then create the table
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
INSERT INTO `search` (`id`, `keyword`) VALUES
(1, 'China's first WEB stop www.bkjia.com'),
(2, 'China's first WEB stop www .bkjia.com'),
(3, 'China's first WEB station www.bkjia.com'),
(4, 'China's first WEB station www.bkjia.com');
Okay, now let’s perform the query operation
*/
if( $_POST) {
$db ='fangke_cc';
mysql tutorial_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']),'
';
}
/*
China’s first WEB station www.111cn.net
China’s first WEB station www.111cn .net
China’s first WEB stop www.111cn.net
China’s first WEB stop
*/
}
?>
Note: This article was originally published on www.bkjia.com Please indicate the source when reprinting