PHP Development _ Multiple keywords, highlighting

Source: Internet
Author: User

Project structure:

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

Search results: Highlighting

Database structure required for the project:

================================================================

Implementation code:

================================================================

conn.php

1 <?php2 $conn = @ mysql_connect ("localhost", "root", "") or Die ("Database link Error"), 3 mysql_select_db ("form", $conn); 4 mysql_qu Ery ("Set names ' GBK '"); 5?>

searchanddisplaywithcolor.php

 1 <?php 2 include ' conn.php '; 3?> 4 5 <table width=500 align= "center" > 6 <form action= "" method= "get" > 7 <tr> 8 &     lt;td> keyword: <input type= "text" name= "KeyWord"/> 9 <input type= "Submit" value= "Search"/></td>10     </tr>11 </form>12 </table>13 <table width=500 border= "0" align= "center" cellpadding= "5" 15 cellspacing= "1" bgcolor= "#add3ef" >16 <?php17//keyword is not empty when the relevant search is performed ($_get[' KeyWord ']) {19//with space characters Key word split open $key =explode ("', $_get[keyword]); $sql =" SELECT * from message where title like ' $key [0] ' or title like ' $key [1] ' or content like ' $key [0] ' or content like '% $key [1]% '], $query =mysql_query ($sql), and while ($row =mysql_ Fetch_array ($query)) {24//replace keyword and highlight keywords $row [Title]=preg_replace ("/$key [0]/i", "<font Color=red&gt ;<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]; >30 &lt  ; TR bgcolor= "#eff3ff" >32 <td> title: <font color= "Black" ><?= $row [title]?></font> Users: <font Color= "BLACK" ><?= $row [user]? ></font>33 <div align= "right" ><a href= "preedit.php?id=<?= $row [id]?> "> Edit </a>&nbsp;&nbsp;|&nbsp;&nbsp;<a34 href=" delete.php?id=<?= $row [         id]?> "> Delete </a></div>35 </td>36 </tr>37 <tr bgcolor=" #ffffff ">38 <td> content: <?= $row [content]?></td>39 </tr>40 <tr bgcolor= "#ffffff" >41 <td&gt ; <div Align= "Right" > Date Published: <?= $row [lastdate]?></div>43 </td>44 </tr>45 <?php}46}47 ? >48 </table>

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//Use a space character to split the key word 2 $key =explode (', $_get[keyword]);

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

PHP Development _ Multiple keywords, highlighting

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.