PHP site search keyword to lighten the implementation method, PHP site search Keywords _php tutorial

Source: Internet
Author: User

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 ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.