PHP implementation _php instance of multi-keyword highlighting function

Source: Internet
Author: User

This article mainly introduces PHP to achieve multiple keyword highlighting function, you can achieve in the search time to highlight reminders, the specific implementation code as follows:

Project structure:

Search results: Highlighting

Database structure required for the project:

Implementation code:

conn.php

<?php 
$conn = @ mysql_connect ("localhost", "root", "") or Die ("Database link Error"); 
mysql_select_db ("form", $conn); 
mysql_query ("Set names ' GBK '"); 

searchanddisplaywithcolor.php

<?php include ' conn.php '; ?> <table width=500 align= "center" > <form action= "" method= "get" > <tr> <td> Keywords: <i Nput type= "text" name= "KeyWord"/> <input type= "Submit" value= "search"/></td> </tr> </form& 
 Gt 
  </table> <table width=500 border= "0" align= "center" cellpadding= "5" cellspacing= "1" bgcolor= "#add3ef" > 
  The <?php//keyword is not empty to perform the relevant search if ($_get[' KeyWord ']) {//spaces the keyword to separate $key =explode (', $_get[keyword]); $sql = "SELECT * from message where title is like ' $key [0] ' or title-like ' $key [1] ' or content like ' $key [0] ' or content like ' 
  % $key [1]% ']; 
  $query =mysql_query ($sql); while ($row =mysql_fetch_array ($query)) {//Replace keywords and highlight keywords $row [Title]=preg_replace ("/$key [0]/i", "<font color= 
   Red><b> $key [0]</b></font> ", $row [title]); $row [Title]=preg_replace ("/$key [0]/i", "<font color=red><b> $key [1]</b></font>", $row [ 
   Title]); $Row[content]=preg_replace ("/$key [0]/i", "<font color=red><b> $key [0]</b></font>", $row [ 
   Content]); $row [Content]=preg_replace ("/$key [1]/i", "<font color=red><b> $key [1]</b></font>", $row [ 
   Content]); ?> <tr bgcolor= "#eff3ff" > <td> title: <font color= "Black" ><?= $row [title]?></font> Users: <font color= "BLACK" ><?= $row [user]?></font> <div align= ' right ' ><a href= ' preedit.php?id=& lt;? = $row [id]?> > Edit </a>  |  <a href= "delete.php?id=<?= $row [id]?>" > Delete </a></div> </td> </tr> <tr bgcolor= "#ffffff" > <td> content: <?= $row [content] ?></td> </tr> <tr bgcolor= "#ffffff" > <td> <div align= "right" > Release date: <?= $ro 
 w[lastdate]?></div> </td> </tr> <?php}}?> </table>

Description: 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 only search a keyword, such as: "Big"

The display will appear garbled ... ^|_|^, this is due to the following code results:

Split the keyword with spaces 
 

I hope this article is helpful to you, PHP implementation of the Multiple keyword highlighting feature content is introduced here. I hope you will continue to pay attention to our website! Want to learn PHP can continue to focus on this site.

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.