XtremeX Posted December 15, 2007 Share Posted December 15, 2007 I have to following but it is really slow, is there a quicker way to accomplish the same thing? $phrases = array ( "a key phrase" => 8, "another key phrase" => 2, "and another key phrase" => 1 ); $badwords = array ( 'a', 'and', 'the', 'while' ); RemoveBadwords($phrases, $badwords); I would like to remove any phrases that contain a badword, this is the current function function RemoveBadwords(&$phrases ,&$badWords) { if (!count($phrases) || !count($badWords)) return; $ret = array(); foreach ($phrases as $phrase=>$count) { // this keepphrase is tripped to false if an unwanted word is found $keepPhrase = true; // paronia check, we need to have a phrase to continue if (strlen($phrase)) { // get the number of words in each array item $wordCount = str_word_count($phrase); if ($wordCount == 1) { // we have one word in this item so simply do a badword check foreach ($badWords as $badword) { if ($phrase == $badword) { // we have a match so we dont want this phrase $keepPhrase = false; break; } } } else { // we have more then one word, so check each one for unwanted words $words = explode(' ', $phrase); foreach ($words as $word) { // check the existance of each badword foreach ($badWords as $badword) { if ($word == $badword) { $keepPhrase = false; break; } } } } // if the phrase doesnt contain unwanted words if ($keepPhrase) { // then add the word to our return array $ret[$phrase] = $count; } } } $phrases = $ret; } Any Ideas? Cheers Phill Link to comment https://forums.phpfreaks.com/topic/81808-badword-filtering-slow/ Share on other sites More sharing options...
rab Posted December 15, 2007 Share Posted December 15, 2007 look at str_replace(). Link to comment https://forums.phpfreaks.com/topic/81808-badword-filtering-slow/#findComment-415624 Share on other sites More sharing options...
XtremeX Posted December 15, 2007 Author Share Posted December 15, 2007 I need to remove the whole element that contains the word, not just replace the word, unless you can give me an example? Cheers Phill Link to comment https://forums.phpfreaks.com/topic/81808-badword-filtering-slow/#findComment-415633 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.