新聞中心
這里有您想知道的互聯網營銷解決方案
看PHP如何實現多關鍵字加亮
項目結構:

開始搜索: 這里搜索關鍵字("大""這")
搜索結果: 高亮顯示
項目所需數據庫結構:
實現代碼:
conn.php
- $conn = @ mysql_connect("localhost", "root", "") or die("數據庫鏈接錯誤");
- mysql_select_db("form", $conn);
- mysql_query("set names 'gbk'");
- ?>
searchAndDisplayWithColor.php
- include 'conn.php';
- ?>
關鍵字:
- cellspacing="1" bgcolor="#add3ef">
- //關鍵字不為空的時候才執(zhí)行相關搜索
- if($_GET['keyWord']){
- //用空格符把關鍵字分割開
- $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)){
- //替換關鍵字,并且把關鍵字高亮顯示
- $row[title]=preg_replace("/$key[0]/i", "$key[0]", $row[title]);
- $row[title]=preg_replace("/$key[0]/i", "$key[1]", $row[title]);
- $row[content]=preg_replace("/$key[0]/i", "$key[0]", $row[content]);
- $row[content]=preg_replace("/$key[1]/i", "$key[1]", $row[content]);
- ?>
標題:=$row[title]?> 用戶:=$row[user] ?> 內容:=$row[content]?> 發(fā)表日期:=$row[lastdate]?>- }
- ?>
說明:在這個小程序中,有一點不足之處在于,只能同時搜索兩個關鍵字,并且中間用空格" "隔開,如果只是搜索一個關鍵字,如:"大"
顯示的時候會出現亂碼 ……^|_|^,這是由于下面代碼的結果:
- //用空格符把關鍵字分割開
- $key=explode(' ', $_GET[keyWord]);
如果要改進的話,在這里的后面就要做一下判斷了。
責任編輯:張偉
來源: Hongten的博客 PHP技術
當前名稱:看PHP如何實現多關鍵字加亮
URL網址:http://m.5511xx.com/article/cdojdig.html


咨詢
建站咨詢
