How does PHP achieve multi-keyword highlighted implementation code: conn. php ??? & Lt ;? Php ???? $ Conn = @ mysql_connect ("localhost", "root", "") ordie ("database link error ");???? Mysql_select_d: How to enable multiple keywords in PHP
Implementation code:
Conn. php
??? ??? $ Conn = @ mysql_connect ("localhost", "root", "") or die ("database link error ");?
??? Mysql_select_db ("form", $ conn );?
??? Mysql_query ("set names 'gbk '");??
??? ?>
SearchAndDisplayWithColor. php
?
?
??? ??? Include 'Conn. php ';?
??? ?>?
???
?
??????
????
$ Key [0]
$ Key [1]
$ Key [0]
$ Key [1]
???????? Cellspacing = "1" bgcolor = "# add3ef">????????? ???????? // When the keyword is not blank, the related search is executed????????? 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 ","", $ Row [title]);????????????? $ Row [title] = preg_replace ("/$ key [0]/I ","", $ Row [title]);????????????? $ Row [content] = preg_replace ("/$ key [0]/I ","", $ Row [content]);????????????? $ Row [content] = preg_replace ("/$ key [1]/I ","", $ 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?
???? $ Key = explode ('', $ _ GET [keyWord]);
If you want to improve the performance, you need to make a judgment later.