Php performs intra-site search and highlights keywords

Source: Internet
Author: User
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.

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.