简体   繁体   中英

PHP search script for multiple words

I have the following issue:

I can't search multiple words.

I have a search engine who search just the full string:

PHP Code:


function ft_search_sidebar() {
  $sidebar[] = array(
    "id" => "search_1",
    "content" => '<div class="section">
        <h2>'.t('Search files &amp; folders').'</h2>
        <form action="" method="post" id="searchform">
            <div>
                <input type="text" name="q" id="q" size="16" value="'.$_REQUEST['q'].'" />
                <input type="button" id="dosearch" value="'.t('Search').'" />
            </div>
            <div id="searchoptions">
                <input type="checkbox" name="type" id="type" checked="checked" /> <label for="type">'.t('Search only this folder and below').'</label>
            </div>
            <div id="searchresults"></div>
        </form>
    </div>'
  );
  return $sidebar;
}


function ft_search_ajax($act) {
  if ($act = '%search%') {
    $new = array();
    $ret = "";
    $q = $_POST['q'];
    $type = $_POST['type'];
    if (!empty($q)) {
        if ($type == "true") {
            $list = _ft_search_find_files(ft_get_dir(), $q);
        } else {
            $list = _ft_search_find_files(ft_get_root(), $q);
        }

Can someone help me?

Grtz

You will need to either develop a search engine which is capable of multi keyword search or simply extract out all the keywords from the search string and then use the search engine multiple times for each keyword. Like this:

$q = $_POST['q'];
$type = $_POST['type'];
if (!empty($q)) {
    if(strpos(trim($q),' ') > 0)
    {
         $array = explode(' ',trim($q));
         foreach($array as $key=>$value)
         {
             //process search multiple times for different values of $value
         }
    }
    else
    {
         if ($type == "true")
         {
             $list = _ft_search_find_files(ft_get_dir(), $q);
         }
         else
         {
             $list = _ft_search_find_files(ft_get_root(), $q);
         }
    } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM