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 ); } } ?> |
...