See how PHP can highlight multiple keywords

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 searching: 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: highlighted

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 by 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. <? Php
  2. $ Conn = @ mysql_connect ("localhost", "root", "") or die ("Database Link error ");
  3. Mysql_select_db ("form", $ conn );
  4. Mysql_query ("set names 'gbk '");
  5. ?>

SearchAndDisplayWithColor. php

 
 
  1. <? Php
  2. Include 'conn. php ';
  3. ?>
  4. <Table width = 500 align = "center">
  5. <Form action = "" method = "get">
  6. <Tr>
  7. <Td> keyWord: <input type = "text" name = "keyWord"/>
  8. <Input type = "submit" value = "Search"/> </td>
  9. </Tr>
  10. </Form>
  11. </Table>
  12. <Tablewidth = 500 border = "0" align = "center" cellpadding = "5"
  13. Cellspacing = "1" bgcolor = "# add3ef">
  14. <? Php
  15. // Search is performed only when the keyword is not empty.
  16. If ($ _ GET ['keyword']) {
  17. // Use a space character to separate keywords
  18. $ Key = explode ('', $ _ GET [keyWord]);
  19. $ SQL = "select * from message where title like '$ key [0]' or title like '$ key [1]' or content like '$ key [0]' or content like '% $ key [1] % '";
  20. $ Query = mysql_query ($ SQL );
  21. While ($ row = mysql_fetch_array ($ query )){
  22. // Replace the keyword and highlight it.
  23. $ Row [title] = preg_replace ("/$ key [0]/I ", "<font color = red> <B> $ key [0] </B> </font>", $ row [title]);
  24. $ Row [title] = preg_replace ("/$ key [0]/I ", "<font color = red> <B> $ key [1] </B> </font>", $ row [title]);
  25. $ Row [content] = preg_replace ("/$ key [0]/I ", "<font color = red> <B> $ key [0] </B> </font>", $ row [content]);
  26. $ Row [content] = preg_replace ("/$ key [1]/I ", "<font color = red> <B> $ key [1] </B> </font>", $ row [content]);
  27. ?>
  28. <Tr bgcolor = "# eff3ff">
  29. <Td> title: <font color = "black"> <? = $ Row [title]?> </Font> User: <font color = "black"> <? = $ Row [user]?> </Font>
  30. <Div align = "right"> <a href = "preEdit. php? Id = <? = $ Row [id]?> "> Edit </a> & nbsp; | & nbsp; <
  31. Href = "delete. php? Id = <? = $ Row [id]?> "> Delete </a> </div>
  32. </Td>
  33. </Tr>
  34. <Tr bgColor = "# ffffff">
  35. <Td> content: <? = $ Row [content]?> </Td>
  36. </Tr>
  37. <Tr bgColor = "# ffffff">
  38. <Td>
  39. <Div align = "right"> posting date: <? = $ Row [lastdate]?> </Div>
  40. </Td>
  41. </Tr>
  42. <? Php}
  43. }
  44. ?>
  45. </Table>

Note: In this small program, one disadvantage is that you can only search for two keywords at the same time and separate them with spaces. If you only search for a keyword, for example, "big"
Garbled characters are displayed ...... ^ | _ | ^, The result of the following code:

 
 
  1. // Use a space character to separate keywords
  2. $ Key = explode ('', $ _ GET [keyWord]);

If you want to improve the performance, you need to make a judgment later.


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.