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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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%'";
      }
    }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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