Database Searching Word Filter

Written something you are proud of, post it here.
Post Reply
wrichards8
Posts: 66
Joined: Thu Jan 12, 2012 3:54 pm
Contact:

Database Searching Word Filter

Post 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

[syntax=php]$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);
[/syntax]
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Database Searching Word Filter

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

[syntax=php]$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]);
}
}[/syntax]
Image
Post Reply