溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

PHP中怎么實(shí)現(xiàn)多關(guān)鍵字加亮

發(fā)布時(shí)間:2021-07-23 16:44:16 來(lái)源:億速云 閱讀:125 作者:Leah 欄目:編程語(yǔ)言

PHP中怎么實(shí)現(xiàn)多關(guān)鍵字加亮,相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。

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

PHP中怎么實(shí)現(xiàn)多關(guān)鍵字加亮

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

PHP中怎么實(shí)現(xiàn)多關(guān)鍵字加亮

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

PHP中怎么實(shí)現(xiàn)多關(guān)鍵字加亮

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

PHP中怎么實(shí)現(xiàn)多關(guān)鍵字加亮

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

conn.php

<?php  $conn = @ mysql_connect("localhost", "root", "") or die("數(shù)據(jù)庫(kù)鏈接錯(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>&nbsp;&nbsp;|&nbsp;&nbsp;<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>

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

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

看完上述內(nèi)容,你們掌握PHP中怎么實(shí)現(xiàn)多關(guān)鍵字加亮的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI