搜索
首页php教程php手册php做站内搜索并高亮显示关键字

本文章介绍一篇较实用的文章关于php做站内搜索并高亮显示关键字,这个很多朋友直接利用preg_replace来操作,这是正确的哦,不过我觉得用str_replace更快一些哦,至于原因自己查查这两个函数的区别吧。

如何使用php做站内搜索并高亮显示关键字?代码如下:

<?php
require_once &#39;sqlTools.class.php&#39;; //封装类,可执行dql、dml语句
$info = $_POST[&#39;info&#39;];
$sql = "select name,password,email from user_500 where name like &#39;%$info%&#39; or password like &#39;%$info%&#39; or email like &#39;%$info%&#39;";
$sqlTools = new SqlTools();
$res = $sqlTools->execute_dql($sql);
while ($row = mysql_fetch_assoc($res)) {
    $row[&#39;name&#39;] = preg_replace("/($info)/i", "<b style=\"color:red\">1</b>", $row[&#39;name&#39;]);
    $row[&#39;password&#39;] = preg_replace("/($info)/i", "<b style=\"color:red\">1</b>", $row[&#39;password&#39;]);
    $row[&#39;email&#39;] = preg_replace("/($info)/i", "<b style=\"color:red\">1</b>", $row[&#39;email&#39;]);
    echo $row[&#39;name&#39;] . "-->" . $row[&#39;password&#39;] . "-->" . $row[&#39;email&#39;] . "<br>";
}
?>

思路分析:将sql语句中包含的%$info%交给DBMS执行的时候,他会查找字段中含有变量$info的值的信息,

%$info--->查找以$info的值结束的信息

$info%--->查找以$info的值开头的信息

通过正则函数preg_replace()将搜索到的关键字高亮显示,比如:

$row['name']=preg_replace("/($info)/i","1",$row['name']);

的意思是:通过POST方接收到的值$info替换为加上样式(红色加粗)的结果,并将结果重新赋给$row[‘name’]

如果要搜索多个关键字的话,可以对接收到值$info进行分割,比如$info_more=explode(" ",$info);//这种方式能对以空格隔开的关键字进行分割,再对分割后的结果挨个进行查询,同样,可以使用正则表达式函数进行替换工作,以高亮显示关键字,代码如下:

sqlTools.class.php的源代码:

<?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("连接数据库失败" . mysql_error());
        }
        mysql_select_db($this->dbname, $this->conn) or die("找不到该数据库" . 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);
    }
}
?>

               
               

本文链接:

收藏随意^^请保留教程地址.

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!