亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 2185 | 回復(fù): 1
打印 上一主題 下一主題

如何使用php做站內(nèi)搜索并高亮顯示關(guān)鍵字? [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2011-12-30 15:31 |只看該作者 |倒序瀏覽
如何使用php做站內(nèi)搜索并高亮顯示關(guān)鍵字?
  1. <?php       require_once 'sqlTools.class.php';//封裝類,可執(zhí)行dql、dml語句       $info=$_POST['info'];       $sql="select name,password,email from user_500 where name like '%$info%' or password like '%$info%' or email like '%$info%'";       $sqlTools=new  SqlTools();       $res=$sqlTools->execute_dql($sql);       while ($row=mysql_fetch_assoc($res)){              $row['name']=preg_replace("/($info)/i","<b style=\"color:red\">\\1</b>",$row['name']);              $row['password']=preg_replace("/($info)/i","<b style=\"color:red\">\\1</b>",$row['password']);              $row['email']=preg_replace("/($info)/i","<b style=\"color:red\">\\1</b>",$row['email']);              echo $row['name']."-->".$row['password']."-->".$row['email']."<br>";       }    ?>
  2. 復(fù)制代碼
復(fù)制代碼
思路分析:

將sql語句中包含的%$info%交給DBMS執(zhí)行的時候,他會查找字段中含有變量$info的值的信息,
%$info--->查找以$info的值結(jié)束的信息
$info%--->查找以$info的值開頭的信息
通過正則函數(shù)preg_replace()將搜索到的關(guān)鍵字高亮顯示,比如,
   
  1. $row['name']=preg_replace("/($info)/i","<b style=\"color:red\">\\1</b>",$row['name']);
復(fù)制代碼
    的意思是:通過POST方接收到的值$info替換為加上樣式(紅色加粗)的結(jié)果,并將結(jié)果重新賦給$row[‘name’]

如果要搜索多個關(guān)鍵字的話,可以對接收到值$info進行分割,比如$info_more=explode(" ",$info);//這種方式能對以空格隔開的關(guān)鍵字進行分割,再對分割后的結(jié)果挨個進行查詢,同樣,可以使用正則表達式函數(shù)進行替換工作,以高亮顯示關(guān)鍵字


sqlTools.class.php的源代碼:
  1. <?php       class SqlTools{              private $host="localhost";              private $dbname="test";              private $dbuser="root";              private $dbpwd="";              private $conn;              public function __construct(){                     $this->conn=mysql_connect($this->host,$this->dbuser,$this->dbpwd);                     if(!$this->conn){                            die("連接數(shù)據(jù)庫失敗".mysql_error());                     }                     mysql_select_db($this->dbname,$this->conn) or die("找不到該數(shù)據(jù)庫".mysql_error());                     mysql_query("set names utf8");              }              public function execute_dml($sql){                     $bool=mysql_query($sql);                     if ($bool){                            if ($bool>0) {                                   return 1;                            }else{                                   return 2;                            }                     }else {                            return 0;                     }              }              public function execute_dql($sql){                     $res=mysql_query($sql);                     return $res;              }              public function close_conn(){                     mysql_close($this->conn);              }       }?>
  2. 復(fù)制代碼
復(fù)制代碼
轉(zhuǎn)載請注明:http://www.cnblogs.com/hongfei/a ... hlight_keyword.html


論壇徽章:
0
2 [報告]
發(fā)表于 2011-12-30 17:21 |只看該作者
有愛的樓主希望與樓至多多交流
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP