新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
看PHP如何實現(xiàn)多關(guān)鍵字加亮
項目結(jié)構(gòu):

開始搜索: 這里搜索關(guān)鍵字("大""這")
搜索結(jié)果: 高亮顯示
項目所需數(shù)據(jù)庫結(jié)構(gòu):
實現(xiàn)代碼:
conn.php
- $conn = @ mysql_connect("localhost", "root", "") or die("數(shù)據(jù)庫鏈接錯誤");
- mysql_select_db("form", $conn);
- mysql_query("set names 'gbk'");
- ?>
searchAndDisplayWithColor.php
- include 'conn.php';
- ?>
關(guān)鍵字:
- cellspacing="1" bgcolor="#add3ef">
- //關(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", "$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] ?> 內(nèi)容:=$row[content]?> 發(fā)表日期:=$row[lastdate]?>- }
- ?>
說明:在這個小程序中,有一點不足之處在于,只能同時搜索兩個關(guān)鍵字,并且中間用空格" "隔開,如果只是搜索一個關(guān)鍵字,如:"大"
顯示的時候會出現(xiàn)亂碼 ……^|_|^,這是由于下面代碼的結(jié)果:
- //用空格符把關(guān)鍵字分割開
- $key=explode(' ', $_GET[keyWord]);
如果要改進的話,在這里的后面就要做一下判斷了。
責任編輯:張偉
來源: Hongten的博客 PHP技術(shù)
分享標題:看PHP如何實現(xiàn)多關(guān)鍵字加亮
網(wǎng)站鏈接:http://fisionsoft.com.cn/article/cdojdig.html


咨詢
建站咨詢
