Page 1 of 1

Database Searching Word Filter

Posted: Mon Feb 27, 2012 1:47 am
by wrichards8
I have developed a database searching script from the tutorial on this site. This is the technique I use to filter out the short words. There is a space at the beginning and end of every word in the array because otherwise it would have filtered out where it matched in the middle of words, so I'd end up with "cody" instead of the word "comedy" in the term array
$wordlist = array(" the ", " at ", " of "," and "." you "," me "," i ");
	$search = trim(mysql_real_escape_string(strtolower($_POST["search"])));
	foreach($wordlist as $word)
	{
		$search = str_replace($word, "", $search);
	}
	$type = trim(mysql_real_escape_string($_POST["type"]));
	$term = preg_split("/[,\s]+/", $search);

Re: Database Searching Word Filter

Posted: Mon Feb 27, 2012 11:49 am
by jacek
Looks good :)

You could also do this, which is basically the same but you don't need to manually define a list of short words to ignore.
$search = trim(mysql_real_escape_string(strtolower($_POST["search"])));
$terms = preg_split("/[,\s]+/", $search);

foreach ($terms as $key => $term){
	if (strlen($term) < 4){
		unset($terms[$key]);
	}
}