Jump to content

Stop Word Filter Code Not working...


Recommended Posts



I have a code to filter out stopwords (from text file) out of a string supplied.


Seems its not filtering out the stop words.  :'(



$CommonWords = file_get_contents('http://minimate.co.uk/stopwords.txt');
$CommonWords = explode("\n", $CommonWords);

$keywords = 'This is did samle a as any ass';

$search_keywords = strtolower(trim($keywords));
$arrWords = explode(' ', $search_keywords);
$arrWords = array_unique($arrWords);

foreach ($arrWords as $word) {
    if (!in_array($word, $CommonWords) && (trim($word) != '')) {
        $searchWords[] = $word;





Output is:

Array ( [0] => this [1] => is [2] => did [3] => sample [4] => a [5] => as [6] => any [7] => ass )


So the output is not filtering out any words from that text file.


Can someone help me with this?



Natasha T

Link to comment
Share on other sites

There's whitespace in the $CommonWords array elements


$CommonWords = file_get_contents('http://minimate.co.uk/stopwords.txt');
$CommonWords = explode("\n", $CommonWords);
$CommonWords = array_map('trim', $CommonWords); // <---- ADD THIS LINE

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...

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.