Jump to content

Search terms!


merylvingien

Recommended Posts

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

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

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.