PHP implementation of SCWS Chinese Word search method

Source: Internet
Author: User
Tags php programming
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.

  • 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.