PHP實(shí)現(xiàn)多關(guān)鍵字加亮功能
本文主要介紹的是PHP實(shí)現(xiàn)多關(guān)鍵字加亮功能,可以實(shí)現(xiàn)在搜索的時(shí)候進(jìn)行高亮提醒,具體實(shí)現(xiàn)代碼如下:
項(xiàng)目結(jié)構(gòu):
搜索結(jié)果: 高亮顯示
項(xiàng)目所需數(shù)據(jù)庫結(jié)構(gòu):
實(shí)現(xiàn)代碼:
conn.php
<?php $conn = @ mysql_connect("localhost", "root", "") or die("數(shù)據(jù)庫鏈接錯(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)鍵字分割開 $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>
說明:在這個(gè)小程序中,有一點(diǎn)不足之處在于,只能同時(shí)搜索兩個(gè)關(guān)鍵字,并且中間用空格" "隔開,如果只是搜索一個(gè)關(guān)鍵字,如:"大"
顯示的時(shí)候會(huì)出現(xiàn)亂碼 ……^|_|^,這是由于下面代碼的結(jié)果:
//用空格符把關(guān)鍵字分割開 $key=explode(' ', $_GET[keyWord]);
希望本文所述對(duì)你有所幫助,PHP實(shí)現(xiàn)多關(guān)鍵字加亮功能內(nèi)容就給大家介紹到這里了。希望大家繼續(xù)關(guān)注我們的網(wǎng)站!想要學(xué)習(xí)php可以繼續(xù)關(guān)注本站。
相關(guān)文章
PHP實(shí)現(xiàn)根據(jù)密碼長(zhǎng)度顯示安全條
本文通過實(shí)例代碼給大家分享php根據(jù)密碼長(zhǎng)度顯示安全條功能,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧2017-07-07關(guān)于PHP的相似度計(jì)算函數(shù):levenshtein的使用介紹
本篇文章小編將為大家介紹,關(guān)于PHP的相似度計(jì)算函數(shù) levenshtein的使用介紹,有需要的朋友可以參考一下2013-04-04PHP實(shí)現(xiàn)阿里大魚短信驗(yàn)證的實(shí)例代碼
本篇文章主要介紹了PHP實(shí)現(xiàn)阿里大魚短信驗(yàn)證的實(shí)例代碼的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07php文件擴(kuò)展名判斷及獲取文件擴(kuò)展名的N種方法
本篇文章主要通過代碼給大家介紹php文件擴(kuò)展名判斷以及獲取文件擴(kuò)展名的N種方法,需要的朋友跟著腳本之家小編一起學(xué)習(xí)學(xué)習(xí)吧2015-09-09laravel 實(shí)現(xiàn)向公共模板中傳值 (view composer)
今天小編就為大家分享一篇laravel 實(shí)現(xiàn)向公共模板中傳值 (view composer),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-10-10PHP--用萬網(wǎng)的接口實(shí)現(xiàn)域名查詢功能
PHP用萬網(wǎng)的接口實(shí)現(xiàn)域名查詢功能,需要的朋友可以了解下2012-12-12