国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 依安县| 安阳县| 丹江口市| 凤山市| 西平县| 航空| 昆明市| 扎囊县| 阿鲁科尔沁旗| 扬中市| 清水河县| 仁化县| 镇宁| 大邑县| 台湾省| 阿合奇县| 囊谦县| 濮阳市| 塘沽区| 确山县| 区。| 方山县| 赤壁市| 香河县| 芦溪县| 甘洛县| 保山市| 叶城县| 河间市| 旅游| 西贡区| 景泰县| 贡山| 千阳县| 内乡县| 桃园县| 哈密市| 江孜县| 罗平县| 桓台县| 汉源县|