natasha_thomas Posted March 4, 2011 Share Posted March 4, 2011 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 Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted March 4, 2011 Share Posted March 4, 2011 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 Quote Link to comment Share on other sites More sharing options...
natasha_thomas Posted March 4, 2011 Author Share Posted March 4, 2011 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 Many Thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.