Jump to content

searching for all but some words


gerkintrigg

Recommended Posts

I was wondering how to ignore words like "if", "and", "in", "where", "or" etc. in order to improve my website searches. I think I know roughly how to do it, using the explode function, but I'd really like to get the syntax right.

here's my code so-far...[code]<?php
include $root.'mydb.php';
echo '<span class="text">';
$search = $_POST['search'];
$explode = explode(" ", $search);
$search = "item_title LIKE '%" . implode("%' OR item_title LIKE '%", $explode) . "%'";
$sql = "SELECT * FROM items WHERE ($search)&& in_stock!='n' ORDER BY item_title ASC";
$result = mysql_query($sql);
if (!$result) {
  // Handle error
  echo 'Query failed. SQL: ', $sql, '<br />Error # ', mysql_errno(), ' Error msg: ', mysql_error();
  exit;
}
while($row = mysql_fetch_assoc($result)) {
$add++;
$add_dic++;
  echo "&nbsp;".$add." <a href='".$root."shop/index.php?open=".$row['item_category']."&itemid=".$row['item_id']."'>".$row['item_title']."</a>", '<br />';
// ---------------------------------

$matches = strip_tags(array());
if (strip_tags(preg_match('/(\S*\s){20}/', strip_tags($row['item_description']), $matches)))
    $excerpt = strip_tags($matches[0]).'...';
else
    $excerpt = strip_tags($row['item_description']);

echo $excerpt.'<BR><BR>';


// ---------------------------------
}
if ($add_dic==0){
echo 'No Records Found In <a href="'.$root.'shop/index.php">The Shop</a>.<BR></span>';
}
?>[/code]

I'd like to be able to say something like: [code]if ($explode!='or'){
//do the rest of the stuff here...
}[/code]
or maybe use a switch statement or some kind, Is there a way to do it a bit like a language filter, where I can search for specific phrases and just remove them?

Thanks.
Link to comment
Share on other sites

[code]

function filter_elements($var) {
     $to_check_strings_array = array("if","and","is","was","or","then");
     if(!in_array(trim($var),$to_check_strings_array)) {
         return($var);
     }
}


$explode = explode(" ", $search);

$explode = array_filter($explode,"filter_elements");

[/code]


I have not tested this code....but something like this should work.
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.