Site Fuse! programming, design, hosting, promotion and advertising tips for webmasters

PHP stop words function

Home  \  Forums  \  Programming & Development Forums  \  PHP Forums  \  PHP stop words function
Share   

user photo

MrPhear   1 years, 6 months ago

This simple php script is a function that takes a list of stop words (commonly occuring words that are of no use to a search engine) and strips them out of a variable term. This is similar to google removal of common words.

<?php
function stopWords($term, $stopwords_file)
{
//load list of common words
$common = file($stopwords_file);
$total = count($common);
for ($x=0; $x<= $total; $x++)
{
$common[$x] = trim(strtolower($common[$x]));
}

//make array of search terms
$_terms = explode(" ", $term);

foreach ($_terms as $line)
{
if (in_array(strtolower(trim($line)), $common))
{
$removeKey = array_search($line, $this->_terms);
unset($_terms[$removeKey]);
}
else
{
$clean_term .= " ".$line;
}
}
return $clean_term;
}
?>
user photo
Guest