《PHP編程:PHP實(shí)現(xiàn)多關(guān)鍵字加亮功能》要點(diǎn):
本文介紹了PHP編程:PHP實(shí)現(xiàn)多關(guān)鍵字加亮功能,希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
PHP教程本文主要介紹的是PHP實(shí)現(xiàn)多關(guān)鍵字加亮功能,可以實(shí)現(xiàn)在搜索的時(shí)候進(jìn)行高亮提醒,具體實(shí)現(xiàn)代碼如下:
PHP教程項(xiàng)目結(jié)構(gòu):
PHP教程
PHP教程搜索結(jié)果:? 高亮顯示
PHP教程
PHP教程項(xiàng)目所需數(shù)據(jù)庫(kù)結(jié)構(gòu):
PHP教程
PHP教程實(shí)現(xiàn)代碼:
PHP教程conn.php
PHP教程
<?php
$conn = @ mysql_connect("localhost", "root", "") or die("數(shù)據(jù)庫(kù)鏈接錯(cuò)誤");
mysql_select_db("form", $conn);
mysql_query("set names 'gbk'");
?>
PHP教程searchAndDisplayWithColor.php
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>
PHP教程說(shuō)明:在這個(gè)小程序中,有一點(diǎn)不足之處在于,只能同時(shí)搜索兩個(gè)關(guān)鍵字,并且中間用空格" "隔開,如果只是搜索一個(gè)關(guān)鍵字,如:"大"
PHP教程顯示的時(shí)候會(huì)出現(xiàn)亂碼 ……^|_|^,這是由于下面代碼的結(jié)果:
PHP教程
//用空格符把關(guān)鍵字分割開
$key=explode(' ', $_GET[keyWord]);
PHP教程希望本文所述對(duì)你有所幫助,PHP實(shí)現(xiàn)多關(guān)鍵字加亮功能內(nèi)容就給大家介紹到這里了.希望大家繼續(xù)關(guān)注我們的網(wǎng)站!想要學(xué)習(xí)php可以繼續(xù)關(guān)注本站.
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/2971.html