Home > Backend Development > PHP Tutorial > Display the implementation code for searching and highlighting keywords in the PHP site

Display the implementation code for searching and highlighting keywords in the PHP site

WBOY
Release: 2016-07-29 08:47:30
Original
1159 people have browsed it

Copy the code The code is as follows:


require_once 'sqlTools.class.php';//Encapsulation class, executable dql, dml statements
$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", "\1",$row['name']);
$row['password']=preg_replace("/($info)/i" ,"\1",$row['password']);
$row['email']=preg_replace("/($info)/i ","\1",$row['email']);
echo $row['name']."-->".$ row['password']."-->".$row['email']."
";
}
?>


Idea analysis:
Change the %$ contained in the sql statement When info% is handed over to the DBMS for execution, it will search for information containing the value of the variable $info in the field,
%$info---> Search for information ending with the value of $info
$info%---> Find information starting with the value of $info
Highlight the searched keywords through the regular function preg_replace(), for example,
  $row['name']=preg_replace("/($info)/i"," \1",$row['name']);
   It means: the value $info received through the POST side is replaced with the added style (red bold) and reassign the result to $row['name']
If you want to search for multiple keywords, you can split the received value $info, such as $info_more=explode(" ",$info );//This method can split keywords separated by spaces, and then query the split results one by one. Similarly, you can use regular expression functions to perform replacement to highlight keywords
sqlTools. Source code of class.php:

Copy the code The code is as follows:


class SqlTools{
private $host="localhost";
private $dbname="test";
private $ dbuser="root";
private $dbpwd="";
private $conn;
public function __construct(){
$this->c
if(!$this->conn){
die("Connect Database failed".mysql_error());
}
mysql_select_db($this->dbname,$this->conn) or die("The database cannot be found".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);
}
}
?>


Original article: WEB Development_Xiao Fei

The above introduces the implementation code for displaying PHP site search and highlighting keywords, including display content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template