floridaflatlander Posted April 13, 2012 Share Posted April 13, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/260865-how-to-build-a-search-for-two-word-searches-using-double-quotes/ Share on other sites More sharing options...
Jessica Posted April 13, 2012 Share Posted April 13, 2012 before you extract them into an array, check if there are " in the string. Quote Link to comment https://forums.phpfreaks.com/topic/260865-how-to-build-a-search-for-two-word-searches-using-double-quotes/#findComment-1337030 Share on other sites More sharing options...
Psycho Posted April 13, 2012 Share Posted April 13, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/260865-how-to-build-a-search-for-two-word-searches-using-double-quotes/#findComment-1337053 Share on other sites More sharing options...
floridaflatlander Posted April 13, 2012 Author Share Posted April 13, 2012 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%'"; } } Quote Link to comment https://forums.phpfreaks.com/topic/260865-how-to-build-a-search-for-two-word-searches-using-double-quotes/#findComment-1337060 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.