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 using str_replace is faster. check the reason for this... 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 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:
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:
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) ;}}?>
Link to this article:
Add to favorites ^ please keep the tutorial address.