Keyword highlighting is relatively simple to implement in PHP. It only needs to obtain and use the extracted keywords for word segmentation and then use the str_replace() function to implement the replacement. The principle is relatively simple.
Project structure:
Start the search: Search keywords here ("big", "this")
Start the search: Search keywords here ("big", "this")
Search results: Highlight
Database structure required for the project:
Database connection file
代码如下 | 复制代码 |
$conn = @ mysql_connect("localhost", "root", "") or die("数据库链接错误"); mysql_select_db("form", $conn); mysql_query("set names 'gbk'"); ?> |
给加色的searchAndDisplayWithColor.php文件
代码如下 | 复制代码 | |||||||
|
Note: In this small program, one shortcoming is that you can only search for two keywords at the same time, and they are separated by a space " ". If you only search for one keyword, such as: "big"
Garbled characters will appear when displayed...^|_|^, which is the result of the following code:
Separate keywords with spaces
The code is as follows
|
Copy code
|
||||
$key=explode(' ', $_GET [keyWord]);
If you want to improve, you have to make a judgment later here. Summary, The above keyword highlighting is just a very simple user submission. We have str_replaced the keyword to highlight it. If you want to do better, you can use the dedecms word segmentation system to segment the words and then operate it. A lot. For the implementation method of dedecms word segmentation program, please refer to A Chinese word segmentation function written in PHP http://www.bKjia.c0m/phper/24/c8b9ff7300e56c042014a655b0bd6e34.htm PHP code for Chinese word segmentation | http://www.bKjia.c0m/phper/php-gj/39302.htm
http://www.bKjia.c0m/phper/php-gj/38506.htm
http: //www.bkjia.com/PHPjc/631628.htmlTechArticleKeyword highlighting is relatively simple to implement in php. He only needs to obtain and use the extracted keywords for word segmentation Then use the str_replace() function to implement the replacement. The principle is relatively simple...