Jump to content

Archived

This topic is now archived and is closed to further replies.

gerkintrigg

searching for all but some words

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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

×

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.