PHP站内搜索:多关键字、加亮显示
1、SQL语句中的模糊查找
$sql = "SELECT * FROM `message` WHERE `content`like '%$k[0]%' and `content`like '%$k[0]%'";
2、多个关键字搜索的原理和技巧
3、替换关键字高亮显示
$r[content] = preg_replace("/($_GET[key])/i", "<font color = red><b>\1</b></font>", $r[content]);
代码实例:
<?php include("26_mysql_class.php"); header("Content-type:text/html;charset=utf-8"); error_reporting(0); $db = new mysql('localhost','root','','message_board','utf8'); if($_GET[key]){ $k = explode ( " ", $_GET[key]); $sql = "SELECT * FROM `message` WHERE `content`like '%$k[0]%' and `content`like '%$k[0]%'"; echo "GET内容:".$_GET[key]."<br>"."<hr>搜索结果<br>"; $q = $db->query($sql); while ($r = mysql_fetch_array($q)) { $r[content] = preg_replace("/($k[0])/i", "<font color = red><b>\1</b></font>", $r[content]); $r[content] = preg_replace("/($k[1])/i", "<font color = red><b>\1</b></font>", $r[content]); echo $r[content]."<br>"; } } ?> <form action="" method="get"> 关键字: <input type = "text" name="key"/> <input type = "submit" name="sub" value="全站搜索"/> </form>
版权声明:本文为博主原创文章,未经博主允许不得转载。