php站內(nèi)搜索并高亮顯示關(guān)鍵字的實(shí)現(xiàn)代碼
更新時(shí)間:2011年12月29日 23:34:03 作者:
將sql語句中包含的%$info%交給DBMS執(zhí)行的時(shí)候,他會(huì)查找字段中含有變量$info的值的信息
復(fù)制代碼 代碼如下:
<?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>";
}
?>
思路分析:
將sql語句中包含的%$info%交給DBMS執(zhí)行的時(shí)候,他會(huì)查找字段中含有變量$info的值的信息,
%$info--->查找以$info的值結(jié)束的信息
$info%--->查找以$info的值開頭的信息
通過正則函數(shù)preg_replace()將搜索到的關(guān)鍵字高亮顯示,比如,
$row['name']=preg_replace("/($info)/i","<b style=\"color:red\">\\1</b>",$row['name']);
的意思是:通過POST方接收到的值$info替換為加上樣式(紅色加粗)的結(jié)果,并將結(jié)果重新賦給$row[‘name']
如果要搜索多個(gè)關(guān)鍵字的話,可以對(duì)接收到值$info進(jìn)行分割,比如$info_more=explode(" ",$info);//這種方式能對(duì)以空格隔開的關(guān)鍵字進(jìn)行分割,再對(duì)分割后的結(jié)果挨個(gè)進(jìn)行查詢,同樣,可以使用正則表達(dá)式函數(shù)進(jìn)行替換工作,以高亮顯示關(guān)鍵字
sqlTools.class.php的源代碼:
復(fù)制代碼 代碼如下:
<?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);
}
}
?>
原創(chuàng)文章:WEB開發(fā)_小飛
您可能感興趣的文章:
- PHP獲取搜索引擎關(guān)鍵字來源的函數(shù)(支持百度和谷歌等搜索引擎)
- PHP自定義函數(shù)獲取搜索引擎來源關(guān)鍵字的方法
- PHP使用微信開發(fā)模式實(shí)現(xiàn)搜索已發(fā)送圖文及匹配關(guān)鍵字回復(fù)的方法
- javascript、php關(guān)鍵字搜索函數(shù)的使用方法
- PHP 搜索查詢功能實(shí)現(xiàn)
- php啟用sphinx全文搜索的實(shí)現(xiàn)方法
- php 搜索框提示(自動(dòng)完成)實(shí)例代碼
- jquery+php實(shí)現(xiàn)搜索框自動(dòng)提示
- ThinkPHP讓分頁保持搜索狀態(tài)的方法
- PHP查找與搜索數(shù)組元素方法總結(jié)
- PHP實(shí)現(xiàn)關(guān)鍵字搜索后描紅功能示例
相關(guān)文章
PHP編程求最大公約數(shù)與最小公倍數(shù)的方法示例
這篇文章主要介紹了PHP編程求最大公約數(shù)與最小公倍數(shù)的方法,涉及php數(shù)學(xué)計(jì)算的相關(guān)運(yùn)算技巧,需要的朋友可以參考下2017-05-05PHP set_time_limit(0)長連接的實(shí)現(xiàn)分析
每次我們?cè)L問PHP腳本的時(shí)候,都是當(dāng)所有的PHP腳本執(zhí)行完成后,我們才得到返回結(jié)果。如果我們需要一個(gè)腳本持續(xù)的運(yùn)行,那么我們就要通過php長連接的方式,來達(dá)到運(yùn)行目的。2010-03-03超級(jí)好用的一個(gè)php上傳圖片類(隨機(jī)名,縮略圖,加水印)
可生成隨機(jī)的名稱,縮略圖,加水印.2010-06-06安裝ImageMagick出現(xiàn)error while loading shared libraries的解決方法
這篇文章主要介紹了安裝ImageMagick出現(xiàn)error while loading shared libraries的解決方法,是ImageMagick安裝與運(yùn)行中經(jīng)常出現(xiàn)的問題,需要的朋友可以參考下2014-09-09