自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

看PHP如何實現(xiàn)多關(guān)鍵字加亮

開發(fā) 后端
在這個小程序中,有一點不足之處在于,只能同時搜索兩個關(guān)鍵字,并且中間用空格" "隔開。但也并非不可改善。

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

開始搜索:   這里搜索關(guān)鍵字("大""這")

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

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

實現(xiàn)代碼:

conn.php

  1. <?php  
  2. $conn = @ mysql_connect("localhost""root"""or die("數(shù)據(jù)庫鏈接錯誤");  
  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>關(guān)鍵字:<input type="text" name="keyWord" />  
  8.          <input type="submit" value="搜索" /></td>  
  9.      </tr>  
  10.      </form>  
  11.  </table>  
  12.    
  13.  <table width=500 border="0" align="center" cellpadding="5" 
  14.      cellspacing="1" bgcolor="#add3ef">  
  15.      <?php  
  16.      //關(guān)鍵字不為空的時候才執(zhí)行相關(guān)搜索  
  17.      if($_GET['keyWord']){  
  18.      //用空格符把關(guān)鍵字分割開  
  19.      $key=explode(' '$_GET[keyWord]);  
  20.      $sql="select * from message where title like '$key[0]' or title like '$key[1]' or content like '$key[0]' or content like '%$key[1]%'";  
  21.      $query=mysql_query($sql);  
  22.      while ($row=mysql_fetch_array($query)){  
  23.          //替換關(guān)鍵字,并且把關(guān)鍵字高亮顯示  
  24.          $row[title]=preg_replace("/$key[0]/i""<font color=red><b>$key[0]</b></font>"$row[title]);  
  25.          $row[title]=preg_replace("/$key[0]/i""<font color=red><b>$key[1]</b></font>"$row[title]);  
  26.          $row[content]=preg_replace("/$key[0]/i""<font color=red><b>$key[0]</b></font>"$row[content]);  
  27.          $row[content]=preg_replace("/$key[1]/i""<font color=red><b>$key[1]</b></font>"$row[content]);  
  28.          ?>  
  29.    
  30.      <tr bgcolor="#eff3ff">  
  31.          <td>標(biāo)題:<font color="black"><?=$row[title]?></font> 用戶:<font color="black"><?=$row[user] ?></font>  
  32.          <div align="right"><a href="preEdit.php?id=<?=$row[id]?>">編輯</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a  
  33.              href="delete.php?id=<?=$row[id]?>">刪除</a></div>  
  34.          </td>  
  35.      </tr>  
  36.      <tr bgColor="#ffffff">  
  37.          <td>內(nèi)容:<?=$row[content]?></td>  
  38.      </tr>  
  39.      <tr bgColor="#ffffff">  
  40.          <td>  
  41.          <div align="right">發(fā)表日期:<?=$row[lastdate]?></div>  
  42.          </td>  
  43.      </tr>  
  44.      <?php }  
  45.      }  
  46.      ?>  
  47.  </table> 

說明:在這個小程序中,有一點不足之處在于,只能同時搜索兩個關(guān)鍵字,并且中間用空格" "隔開,如果只是搜索一個關(guān)鍵字,如:"大"
顯示的時候會出現(xiàn)亂碼 ……^|_|^,這是由于下面代碼的結(jié)果:

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

如果要改進(jìn)的話,在這里的后面就要做一下判斷了。

 

責(zé)任編輯:張偉 來源: Hongten的博客
相關(guān)推薦

2009-11-26 19:24:54

PHP類CMS

2009-12-10 13:23:29

PHP關(guān)鍵字this

2010-10-08 15:37:21

MySQL單表

2009-12-10 13:31:20

PHP self關(guān)鍵字

2009-12-08 18:02:06

PHP final關(guān)鍵

2009-11-30 10:23:35

fixed關(guān)鍵字

2009-12-03 19:06:44

PHP關(guān)鍵字globa

2009-08-21 14:58:56

C# this關(guān)鍵字

2013-01-30 10:12:14

Pythonyield

2018-04-20 15:56:09

Pythonglobal關(guān)鍵字

2009-09-02 09:24:03

C# this關(guān)鍵字

2009-09-17 09:30:00

Linq LET關(guān)鍵字

2012-03-01 12:50:03

Java

2022-01-04 16:35:42

C++Protected關(guān)鍵字

2010-02-06 10:09:47

C++模擬event關(guān)

2009-08-06 17:52:23

C#增加that關(guān)鍵字

2019-12-20 15:19:41

Synchroinze線程安全

2022-01-10 18:11:42

C語言應(yīng)用技巧

2024-03-15 15:12:27

關(guān)鍵字底層代碼

2009-08-13 17:44:34

C# using關(guān)鍵字
點贊
收藏

51CTO技術(shù)棧公眾號