PHP site search keyword to lighten the implementation method, the search keywords in the PHP site
In this paper, the implementation of search keywords in PHP station is described. Share to everyone for your reference. The specific analysis is as follows:
Our practice is to put the search results out, with the search keyword the same replacement Cheng Gaoliang Word, we will use the Str_replace (you find the keyword, <带有高亮的html标签> you find the keyword, $str);
It's so easy, okay. Let's take a look at an example.
First, create a database: ' Searchkey ';
Create the table again, the SQL repository code is as follows:
Copy the code code as follows: CREATE table ' fangke_cc '. ' Search ' (
' id ' INT (4) Not NULL auto_i ncrement,
' keyword ' VARCHAR (not NULL),
PRIMARY KEY (' id ')
) ENGINE = MYISAM
We import some data, the SQL build library code is as follows:
copy code code is as follows: INSERT into ' search ' (' id ', ' keyword ') VALUES
(1, ' Chinese scripting first station www.jb51.net '),
(2, ' Chinese script programming first station www.jb51.net '),
(3, ' Chinese script programming first station www.jb51.net '),
(4, ' Chinese script programming first station www.jb51.net ');
Okay, let's do the query, here's the example code: the
copy Code 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 is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/935488.html www.bkjia.com true http://www.bkjia.com/PHPjc/935488.html techarticle PHP Station search keyword to lighten the implementation method, PHP search keywords in the site example of the search engine in the PHP site to lighten the implementation method. Share to everyone for your reference. Specific points ...