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 Link to comment https://forums.phpfreaks.com/topic/229548-stop-word-filter-code-not-working/ 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 Link to comment https://forums.phpfreaks.com/topic/229548-stop-word-filter-code-not-working/#findComment-1182655 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! Link to comment https://forums.phpfreaks.com/topic/229548-stop-word-filter-code-not-working/#findComment-1182658 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.