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