Keyword highlighting in PHP is relatively simple to achieve, he only need to obtain the use of the extracted keyword word segmentation and then use the Str_replace () function to implement the replacement can be relatively simple principle.
Project structure:
Start Search: Search for keywords here ("big" "This")
Start Search: Search for keywords here ("big" "This")
Search results: Highlighting
Database structure required for the project:
Database Connection File
The code is as follows |
Copy Code |
$conn = @ mysql_connect ("localhost", "root", "") or Die ("Database link error"); mysql_select_db ("form", $conn); mysql_query ("Set names ' GBK '"); ?> |
To add color to the searchanddisplaywithcolor.php file
The code is as follows |
Copy Code |
Include ' conn.php '; ?>
cellspacing= "1" bgcolor= "#add3ef" > The keyword is not empty when you perform a related search if ($_get[' KeyWord ']) { Divide the key with a space character $key =explode (', $_get[keyword]); $sql = "SELECT * from the 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 the keyword $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: |
Published date:
|
}?>
Note: In this small program, a little bit of a disadvantage is that only two keywords can be searched at the same time, and the middle with a space "" separated, if you just search a keyword, such as: "Big"
The display will appear garbled ... ^|_|^, this is due to the result of the following code:
Divide the key with a space character
The code is as follows |
Copy Code |
$key =explode (', $_get[keyword]);
|
If we want to improve, we should make a judgment in the back of this place.
Summarize
The above keyword highlighting is just a very simple user to submit what we have done on this keyword str_replace to highlight, if you want to do better can use the Dedecms word breaker will be a lot more good.
The implementation method of DEDECMS Word segmentation program can be referenced
A Chinese word function written in PHP
Http://www.bKjia.c0m/phper/24/c8b9ff7300e56c042014a655b0bd6e34.htm
PHP Code for Chinese participle
Http://www.bKjia.c0m/phper/php-gj/39302.htm
Simple Chinese word code made by PHP
Http://www.bKjia.c0m/phper/php-gj/38506.htm
http://www.bkjia.com/PHPjc/631628.html www.bkjia.com true http://www.bkjia.com/PHPjc/631628.html techarticle keyword highlighting in PHP is relatively simple to achieve, he only need to obtain the use of the extracted keyword word segmentation and then use the Str_replace () function to implement the replacement can be relatively ...