Jump to content

How to build a search for two word searches using double quotes?


floridaflatlander

Recommended Posts

I am now using an underscore for a two word search (Grizzly_Bear) it seems to do a good job but  I noticed the standard is double quotes “Grizzly Bear”

 

So how to build a search for two word searches using double quotes? This is what I have so far ...

$user_search = mysqli_real_escape_string($dbc, $_GET['usersearch']);

$search_query = "SELECT * FROM db_table";

// Extract the search keywords into an array
    $clean_search = str_replace(',', ' ', $user_search);
    $search_words = explode(' ', $clean_search);
    $final_search_words = array();
    if (count($search_words) > 0) {
      foreach ($search_words as $word) {
        if (!empty($word)) {
		$trimmed_word = trim($word);
		$trimmed_word = str_replace('_', ' ', $trimmed_word);
		$final_search_words[] = $trimmed_word;
        }
      }
    }

    // Generate a WHERE clause using all of the search keywords
    $where_list = array();
    if (count($final_search_words) > 0) {
      foreach($final_search_words as $word) {
        $where_list[] = "table_column LIKE '%$word%'";
      }
    }

    $where_clause = implode(' OR ', $where_list);

    // Add the keyword WHERE clause to the search query
    if (!empty($where_clause)) {
      $search_query .= " WHERE $where_clause"; // This is the final query
    }

 

Thanks

SJ

I've done this before a long time ago. The one thing you need to decide is how to handle when there are an uneven number of quotes. The most logical approach IMO is to start from the front and if there is an orphan quote, just discard it. Plus, do you want to allow single & double quotes or just one?

Wow, talk about a jury rig.

 

Anyway, I borrowed/took this preg_match from stack flow. As I walk through this I get, table_column LIKE '%"Grizzly Bear"%'  and I can't remove the double quotes in  '%"Grizzly Bear"%'

    // Extract the search keywords into an array
    $clean_search = str_replace(',', ' ', $user_search);
    preg_match('"([^\\"]+)"', $clean_search, $multi_words); // added
    
    $clean_search = str_replace("$multi_words[0]", ' ', $clean_search); // I'll make this a loop later
    $search_words = explode(' ', $clean_search);
    $final_search_words = array();
    if (count($search_words) > 0) {
      foreach ($search_words as $word) {
        if (!empty($word)) {
$final_search_words[] = $word;
        }
      }
    }

$final_search_words[] = $multi_words[0];

    // Generate a WHERE clause using all of the search keywords
    $where_list = array();
    if (count($final_search_words) > 0) {
      foreach($final_search_words as $word) {
       $word = trim($word);
       $word = str_replace('"', '', $word); // I can get this to work for an underscore but not for double quotes for some reason
       $where_list[] = "table_column LIKE '%$word%'";
      }
    }

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.