欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP實現(xiàn)多關(guān)鍵字加亮功能

 更新時間:2016年10月21日 15:54:20   作者:Hongten  
關(guān)鍵字加亮可以更好的幫助閱讀,本篇文章主要PHP實現(xiàn)多關(guān)鍵字加亮功能,具有一定的參考價值,有需要的可以看一下。

本文主要介紹的是PHP實現(xiàn)多關(guān)鍵字加亮功能,可以實現(xiàn)在搜索的時候進(jìn)行高亮提醒,具體實現(xiàn)代碼如下:

項目結(jié)構(gòu):

搜索結(jié)果:  高亮顯示

項目所需數(shù)據(jù)庫結(jié)構(gòu):

實現(xiàn)代碼:

conn.php

<?php 
$conn = @ mysql_connect("localhost", "root", "") or die("數(shù)據(jù)庫鏈接錯誤"); 
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>關(guān)鍵字:<input type="text" name="keyWord" /> 
   <input type="submit" value="搜索" /></td> 
  </tr> 
  </form> 
 </table> 
 
 <table width=500 border="0" align="center" cellpadding="5" 
  cellspacing="1" bgcolor="#add3ef"> 
  <?php 
  //關(guān)鍵字不為空的時候才執(zhí)行相關(guān)搜索 
  if($_GET['keyWord']){ 
  //用空格符把關(guān)鍵字分割開 
  $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); 
  while ($row=mysql_fetch_array($query)){ 
   //替換關(guān)鍵字,并且把關(guān)鍵字高亮顯示 
   $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>標(biāo)題:<font color="black"><?=$row[title]?></font> 用戶:<font color="black"><?=$row[user] ?></font> 
   <div align="right"><a href="preEdit.php?id=<?=$row[id]?>">編輯</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a 
    href="delete.php?id=<?=$row[id]?>">刪除</a></div> 
   </td> 
  </tr> 
  <tr bgColor="#ffffff"> 
   <td>內(nèi)容:<?=$row[content]?></td> 
  </tr> 
  <tr bgColor="#ffffff"> 
   <td> 
   <div align="right">發(fā)表日期:<?=$row[lastdate]?></div> 
   </td> 
  </tr> 
  <?php } 
  } 
  ?> 
 </table> 

說明:在這個小程序中,有一點不足之處在于,只能同時搜索兩個關(guān)鍵字,并且中間用空格" "隔開,如果只是搜索一個關(guān)鍵字,如:"大"

顯示的時候會出現(xiàn)亂碼 ……^|_|^,這是由于下面代碼的結(jié)果:

//用空格符把關(guān)鍵字分割開 
 $key=explode(' ', $_GET[keyWord]); 

希望本文所述對你有所幫助,PHP實現(xiàn)多關(guān)鍵字加亮功能內(nèi)容就給大家介紹到這里了。希望大家繼續(xù)關(guān)注我們的網(wǎng)站!想要學(xué)習(xí)php可以繼續(xù)關(guān)注本站。

相關(guān)文章

最新評論