gerkintrigg Posted April 26, 2006 Share Posted April 26, 2006 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 " ".$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. Quote Link to comment Share on other sites More sharing options...
samshel Posted April 26, 2006 Share Posted April 26, 2006 [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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.