Jump to content

Stop Word Filter Code Not working...


natasha_thomas
 Share

Recommended Posts

Friends,

 

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

 

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

 

<?php

$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;
    } 

}
print_r($searchWords);

?>

 

 

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?

 

Cheers

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.

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

 Share

×
×
  • 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.