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.
http://www.bkjia.com/PHPjc/975879.htmlwww.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...