Php performs intra-site search and highlights keywords _ PHP Tutorial

Source: Internet
Author: User
Php performs intra-site search and highlights keywords. This article introduces a more practical article about how php performs intra-site search and highlights keywords. many friends use preg_replace directly, which is correct, however, I think it is correct to use this article to introduce a more practical article about how php performs intra-site search and highlights keywords. many friends use preg_replace directly, however, I think using str_replace is faster. Check the differences between the two functions for the reason.

How can I use php for intra-site search and highlight keywords?

The code is as follows:

Require_once 'sqltools. class. php'; // encapsulation class, which can execute dql and 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 ']."
";

}

?>

Train of Thought analysis:

When the SQL statement contains % $ info % and is handed over to the DBMS for execution, it will find information about the value of $ info in the field,

% $ Info ---> search for information ending with $ info

$ Info % ---> search for information starting with $ info

Use the regular function preg_replace () to highlight the searched keywords, for example,

$ Row ['name'] = preg_replace ("/($ info)/I ","\ 1", $ Row ['name']);

The value $ info received by the POST Party is replaced with the result of adding the style (red bold), and the result is re-assigned to $ row ['name']

To search for multiple keywords, you can split the received value $ info, for example, $ info_more = explode ("", $ info ); // This method can be used to split keywords separated by spaces, and then query the split results one by one. Similarly, you can use a regular expression function to replace the results to highlight keywords.

The code is as follows:

Source code of sqlTools. class. 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 ("failed to connect to database". mysql_error ());

}

Mysql_select_db ($ this-> dbname, $ this-> conn) or die ("this 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 );

}

}

?>

...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.