使用PHP怎么實現(xiàn)一個多關(guān)鍵字加亮功能?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
創(chuàng)新互聯(lián)自2013年起,先為北關(guān)等服務(wù)建站,北關(guān)等地企業(yè),進行企業(yè)商務(wù)咨詢服務(wù)。為北關(guān)企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。項目結(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>標題:<font color="black"><?=$row[title]?></font> 用戶:<font color="black"><?=$row[user] ?></font> <div align="right"><a href="preEdit.php?id=<?=$row[id]?>">編輯</a> | <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]);
看完上述內(nèi)容,你們掌握使用PHP怎么實現(xiàn)一個多關(guān)鍵字加亮功能的方法了嗎?如果還想學到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
本文名稱:使用PHP怎么實現(xiàn)一個多關(guān)鍵字加亮功能-創(chuàng)新互聯(lián)
路徑分享:http://bm7419.com/article46/cdgdeg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊、網(wǎng)站建設(shè)、營銷型網(wǎng)站建設(shè)、App設(shè)計、商城網(wǎng)站、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)