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

