merylvingien Posted June 6, 2010 Share Posted June 6, 2010 Hi folks, i have implemented a search page on my site which works ok, but it only returns results for searches which are matched identically. For example, if i search for "rubber ducky in bath" it will only return a result where that exact phrase is present within the database. I am wondering if there is a way of loosening this up a bit, so if i searched "bath" it will return the same result? The code i have is this: if (isset($_GET['search'])) { $searchTerms = trim($_GET['search']); $searchTerms = strip_tags($searchTerms); // remove any html/javascript. if (strlen($searchTerms) < 3) { $error[] = "Search terms must be longer than 3 characters."; }else { $searchTermDB = mysql_real_escape_string($searchTerms); // prevent sql injection. } // If there are no errors, lets get the search going. if (count($error) < 1) { $searchSQL = "SELECT stitle, varia, sbody FROM simp_search WHERE `sbody` LIKE '%{$searchTermDB}%' OR `varia` LIKE '%{$searchTermDB}%' OR `stitle` LIKE '%{$searchTermDB}%' ORDER BY `stitle`"; Link to comment https://forums.phpfreaks.com/topic/204019-search-terms/ Share on other sites More sharing options...
ngoweb Posted June 6, 2010 Share Posted June 6, 2010 Basically you have to split the string into individual words - then build your or statement from that array. $string = "This is string sample"; $split_point = " "; $split_string = explode($split_point, $string); /* which will give you $split_string[0] = 'This' $split_string[1] = 'is' etc */ Link to comment https://forums.phpfreaks.com/topic/204019-search-terms/#findComment-1068584 Share on other sites More sharing options...
teynon Posted June 6, 2010 Share Posted June 6, 2010 In addition to that, I would make sure you still search for the full term to ensure accuracy and then individual words for more options. Link to comment https://forums.phpfreaks.com/topic/204019-search-terms/#findComment-1068632 Share on other sites More sharing options...
merylvingien Posted June 6, 2010 Author Share Posted June 6, 2010 Thanks folks, i will have a play about with it Link to comment https://forums.phpfreaks.com/topic/204019-search-terms/#findComment-1068665 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.