See how PHP implements multi-keyword highlighting _php tutorials

Source: Internet
Author: User
Project structure:

498) this.width=498; ' OnMouseWheel = ' javascript:return big (This) ' height= "183" alt= "\" width= "274" src= "/HTTP/ Www.bkjia.com/uploadfile/2013/0904/20130904095406958.png "/>

Start Search: Search for keywords here ("big" "This")

498) this.width=498; ' OnMouseWheel = ' javascript:return big (This) ' height= "269" alt= "\" width= "640" src= "/HTTP/ Www.bkjia.com/uploadfile/2013/0904/20130904095406563.png "/>

Search results: Highlighting

498) this.width=498; ' OnMouseWheel = ' javascript:return big (This) ' height= "480" alt= "\" width= "565" src= "/HTTP/ Www.bkjia.com/uploadfile/2013/0904/20130904095406399.png "/>

Database structure required for the project:

498) this.width=498; ' OnMouseWheel = ' javascript:return big (This) ' height= "187" alt= "\" width= "640" src= "/HTTP/ Www.bkjia.com/uploadfile/2013/0904/20130904095406831.png "/>

Implementation code:

conn.php

 
  
  
  1. $conn = @ mysql_connect ("localhost""root " "" or die("Database link Error");
  2. mysql_select_db ("form"$conn);
  3. mysql_query ("set names ' GBK '");

searchanddisplaywithcolor.php

 
 
  1. include ' conn.php ';
  2. ?>
  3. "Center" >
  4. "0"align="Center"cellpadding="5"
  5. cellspacing= "1" bgcolor= "#add3ef" >
  6. //keyword is not empty when performing a related search
  7. if ($_get[' KeyWord ']) {
  8. //Use space to split the keyword
  9. $key = Explode (', $_get[KeyWord]);
  10. $sql = "SELECT * from message where the title like ' $key [0] ' or title like ' $key [1] ' or content like ' $key [0] ' or content like ' % $key [1]% '];
  11. $query =mysql_query ($sql);
  12. while ($row=mysql_fetch_array ($query)) {
  13. //Replace keywords and highlight keywords
  14. $row [Title]=preg_replace ("/$key [0]/i", "$key [0]", $row [title]);
  15. $row [Title]=preg_replace ("/$key [0]/i", "$key [1]", $row [title]);
  16. $row [Content]=preg_replace ("/$key [0]/i", "$key [0]", $row[content]);
  17. $row [Content]=preg_replace ("/$key [1]/i", "$key [1]", $row[content]);
  18. ?>
  19. "#eff3ff">
  20. "#ffffff">
  21. "#ffffff">
  22. }
  23. ?>
  24. Title:"Black"> $row[title]?> User:"BLACK"> $row [User]?>
  25. "Right" >"preedit.php?id= "> Edit |
  26. href= "delete.php?id= "> Delete
  27. Content: $row[content]?>
  28. "Right" > Published date: $row [lastdate]?>

Note: In this small program, a little bit of a disadvantage is that only two keywords can be searched at the same time, and the middle with a space "" separated, if you just search a keyword, such as: "Big"
The display will appear garbled ... ^|_|^, this is due to the result of the following code:

 
  
  
  1. $key = Explode ('$_get

If we want to improve, we should make a judgment in the back of this place.


http://www.bkjia.com/PHPjc/445701.html www.bkjia.com true http://www.bkjia.com/PHPjc/445701.html techarticle Project structure: 498) this.width=498; ' OnMouseWheel = ' javascript:return big (This) ' height= "183" alt= "\" width= "274" src= "http ://www.bkjia.com/uploadfile/2013/0904/2013090409540695 ...

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