In this paper, we describe the method of PHP implementation SCWS Chinese word search. Share to everyone for your reference, as follows:
1, 4 files (the download address of this site.) ) after decompression, put it in a place EG:E:/WAMP/SCWS
2, php.ini in the configuration
Extension = Php_scws.dll Scws.default.charset = UTF8//Configure the default encoding method Scws.default.fpath = "E:/WAMP/SCWS"//The path of the file after pressurization
3. Use
$so = Scws_new (); $so->set_charset (' UTF8 '); Code//There is no call to Set_dict and set_rule the system automatically tries to invoke the dictionary and rule file under the specified path in INI $so->add_dict (ini_get (' Scws.default.fpath '). '/dict.utf8.xdb ');//$so->add_dict ('./dd.txt ', scws_xdict_txt); $so->set_rule (Ini_get (' Scws.default.fpath ') . '/rules.utf8.ini '); $so->set_duality (0); Two Yuan $so->set_ignore (0); Ignore punctuation $so->set_multi (0); $WD = ' What to search '; $so->send_text ($WD); while ($tmp = $so->get_result ())//After the traversal, the content of the string is divided,//based on the resulting content into the database to find (like match) {}
Personally experimented with the following:
<title>Test</title><?phpfunction Search ($keywords, $table, $key 1, $key 2) {//(string to find, table name, field one, field two) if it is a field, $key 2= '; $arr =array (); $ Str=array (); if (Preg_match ("/^[^\x80-\xff]+$/", $keywords)) {//To determine whether the keywords is all in English, all in English $str 1=explode ("', $keywords) in accordance with the string cut; Cut the foreach ($str 1 as $key + $value) by the space first {$str 2=explode (', ', $value);//, Cut foreach ($str 2 as $k + = $v) {$str 3=explo De ('. ', $v); 。 Cut foreach ($str 3 as $kk = + $vv) {Array_push ($arr, $VV);//after processing, return an array}}}}else{//word processing for Chinese $so = scws_new (); $so->set_charset (' UTF8 '); There is no call to Set_dict and set_rule the system automatically tries to invoke the dictionary and rule file under the specified path in INI $so->add_dict (ini_get (' Scws.default.fpath '). '/dict.utf8.xdb '); $so->set_rule (Ini_get (' Scws.default.fpath '). '/rules.utf8.ini '); $so->set_duality (0); Two Yuan $so->set_ignore (0); Ignore punctuation $so->set_multi (0); $so->send_text ($keywords); while ($tmp = $so->get_result ())//Get an array containing the phrase and punctuation {foreach ($tmp as $key = = $value) {$value = $value ["word"]; if (Preg_match ("/^[-Calls]{6,21}$/", $value))//Remove punctuation mark {array_push ($arr, $value); }}}} ($arr as $key + $value)//convenient array to match {if ($key 2!= ") {$sql =" select * from ' $table ' where ' $key 1 ' l Ike '% $value% ' or ' $key 2 ' like '% $value ' "; }else{$sql = "select * from ' $table ' where ' $key 1 ' like '% $value% '";} $query =mysql_query ($sql); Array_push ($str, $query); } return $str;} Include (' conn.php '); $keywords = "haha LSD djk,hdjs dd"; $table = ' Two_key '; $key 1= ' address '; $key 2= '; $query =search ($ Keywords, $table, $key 1, $key 2), foreach ($query as $key + = $value) {while ($row =mysql_fetch_array ($value)) {echo $row [ ID]. ' ----'. $row [name]. ' -----'. $row [address]. '
'; }}?>
I hope this article is helpful to you in PHP programming.
The above describes the PHP implementation of SCWS Chinese word search method, including the content, I hope the PHP tutorial interested in a friend helpful.