码迷,mamicode.com
首页 > Web开发 > 详细

1.PHP站内搜索

时间:2015-07-31 23:15:08      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:

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>

 

1.PHP站内搜索

标签:

原文地址:http://www.cnblogs.com/chenyigen/p/4693342.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!