使用PHP怎么實(shí)現(xiàn)一個(gè)多關(guān)鍵字加亮功能?相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。
創(chuàng)新互聯(lián)自2013年起,先為北關(guān)等服務(wù)建站,北關(guān)等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為北關(guān)企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。項(xiàng)目結(jié)構(gòu):
搜索結(jié)果: 高亮顯示
項(xiàng)目所需數(shù)據(jù)庫(kù)結(jié)構(gòu):
實(shí)現(xiàn)代碼:
conn.php
<?php $conn = @ mysql_connect("localhost", "root", "") or die("數(shù)據(jù)庫(kù)鏈接錯(cuò)誤"); 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)鍵字不為空的時(shí)候才執(zhí)行相關(guān)搜索 if($_GET['keyWord']){ //用空格符把關(guān)鍵字分割開(kāi) $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> | <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>
說(shuō)明:在這個(gè)小程序中,有一點(diǎn)不足之處在于,只能同時(shí)搜索兩個(gè)關(guān)鍵字,并且中間用空格" "隔開(kāi),如果只是搜索一個(gè)關(guān)鍵字,如:"大"
顯示的時(shí)候會(huì)出現(xiàn)亂碼 ……^|_|^,這是由于下面代碼的結(jié)果:
//用空格符把關(guān)鍵字分割開(kāi) $key=explode(' ', $_GET[keyWord]);
看完上述內(nèi)容,你們掌握使用PHP怎么實(shí)現(xiàn)一個(gè)多關(guān)鍵字加亮功能的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
本文名稱:使用PHP怎么實(shí)現(xiàn)一個(gè)多關(guān)鍵字加亮功能-創(chuàng)新互聯(lián)
路徑分享:http://muchs.cn/article46/cdgdeg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊(cè)、網(wǎng)站建設(shè)、營(yíng)銷型網(wǎng)站建設(shè)、App設(shè)計(jì)、商城網(wǎng)站、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)