Php: search for multiple keywords highlighted program code _ PHP Tutorial

Source: Internet
Author: User
Php allows you to search for program code with multiple keywords highlighted. Keyword Highlighting is relatively easy to implement in php. he only needs to obtain the extracted keywords for word segmentation and then use str_replace () function to implement Replacement. The principle is relatively strong. keyword highlighting is relatively simple in php. he only needs to obtain the extracted keywords for word segmentation and then use str_replace () functions can be replaced in a relatively simple way.

Project structure:

Start searching: search for keywords here ("big" "this ")

Start searching: search for keywords here ("big" "this ")

Search results: highlighted

Database structure required by the project:

Database Connection file

The code is as follows:
$ Conn = @ mysql_connect ("localhost", "root", "") or die ("database link error ");
Mysql_select_db ("form", $ conn );
Mysql_query ("set names 'gbk '");
?>

SearchAndDisplayWithColor. php file to be colored












}?>
The code is as follows:

Include 'Conn. php ';
?>





Cellspacing = "1" bgcolor = "# add3ef">
// Search is performed only when the keyword is not empty.
If ($ _ GET ['keyword']) {
// Use a space character to separate keywords
$ Key = explode ('', $ _ GET [keyWord]);
$ SQL = "select * from message where title like '$ key [0]' or title like '$ key [1]' or content like '$ key [0]' or content like '% $ key [1] % '";
$ Query = mysql_query ($ SQL );
While ($ row = mysql_fetch_array ($ query )){
// Replace the keyword and highlight it.
$ Row [title] = preg_replace ("/$ key [0]/I ","$ Key [0]", $ Row [title]);
$ Row [title] = preg_replace ("/$ key [0]/I ","$ Key [1]", $ Row [title]);
$ Row [content] = preg_replace ("/$ key [0]/I ","$ Key [0]", $ Row [content]);
$ Row [content] = preg_replace ("/$ key [1]/I ","$ Key [1]", $ Row [content]);
?>

Title: User:

"> Edit | href =" delete. php? Id = "> Delete


Content:

Posting Date:


Note: in this small program, one disadvantage is that you can only search for two keywords at the same time and separate them with spaces. if you only search for a keyword, for example, "big"
Garbled characters are displayed ...... ^ | _ | ^, The result of the following code:

Use a space character to separate keywords

The code is as follows:
$ Key = explode ('', $ _ GET [keyWord]);

If you want to improve the performance, you need to make a judgment later.

Conclusion,

The keyword highlighted above is just a very simple one. we have highlighted the keyword str_replace, if you want to do better, you can use the dedecms word splitting system to perform word segmentation and perform further operations.

For the dedecms word segmentation program implementation method, refer

A Chinese word segmentation function written in PHP

The http://www.bKjia. c0m/phper/24/c8b9ff7300e56c042014a655b0bd6e34.htm

Php code for Chinese word segmentation

Http://www.bKjia. c0m/phper/php-gj/39302.htm

Php simple Chinese word segmentation code

Http://www.bKjia. c0m/phper/php-gj/38506.htm

The replacement by using the lift () function works relatively well...

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.