国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開發(fā) > PHP > 正文

php 多關(guān)鍵字 高亮顯示實(shí)現(xiàn)代碼

2024-05-04 23:15:03
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

項(xiàng)目結(jié)構(gòu):

php 多關(guān)鍵字 高亮顯示實(shí)現(xiàn)代碼

開始搜索:   這里搜索關(guān)鍵字("大""這")

php 多關(guān)鍵字 高亮顯示實(shí)現(xiàn)代碼

搜索結(jié)果:  高亮顯示

php 多關(guān)鍵字 高亮顯示實(shí)現(xiàn)代碼

項(xiàng)目所需數(shù)據(jù)庫(kù)結(jié)構(gòu):

php 多關(guān)鍵字 高亮顯示實(shí)現(xiàn)代碼


實(shí)現(xiàn)代碼:
conn.php

復(fù)制代碼 代碼如下:


<?php
$conn = @ mysql_connect("localhost", "root", "") or die("數(shù)據(jù)庫(kù)鏈接錯(cuò)誤");
mysql_select_db("form", $conn);
mysql_query("set names 'gbk'");
?>


searchAndDisplayWithColor.php

復(fù)制代碼 代碼如下:


<?php
include 'conn.php';
?>

<table width=500>
<form action="" method="get">
<tr>
<td>關(guān)鍵字:<input type="text" />
<input type="submit" value="搜索" /></td>
</tr>
</form>
</table>

<table width=500 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><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>發(fā)表日期:<?=$row[lastdate]?></div>
</td>
</tr>
<?php }
}
?>
</table>


說(shuō)明:在這個(gè)小程序中,有一點(diǎn)不足之處在于,只能同時(shí)搜索兩個(gè)關(guān)鍵字,并且中間用空格" "隔開,如果只是搜索一個(gè)關(guān)鍵字,如:"大"
顯示的時(shí)候會(huì)出現(xiàn)亂碼 ……^|_|^,這是由于下面代碼的結(jié)果:

復(fù)制代碼 代碼如下:


//用空格符把關(guān)鍵字分割開
$key=explode(' ', $_GET[keyWord]);


如果要改進(jìn)的話,在這里的后面就要做一下判斷了。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 黑水县| 缙云县| 铁岭市| 湖北省| 逊克县| 南木林县| 花莲县| 敦煌市| 寿宁县| 双柏县| 巧家县| 泽库县| 克东县| 贞丰县| 会泽县| 白沙| 澄迈县| 铜川市| 馆陶县| 山丹县| 麻城市| 泸西县| 平凉市| 鄂伦春自治旗| 遂宁市| 彩票| 凌海市| 张家口市| 穆棱市| 乾安县| 交城县| 泸定县| 开封市| 新宾| 岳阳市| 文成县| 大田县| 金沙县| 运城市| 徐汇区| 江孜县|