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 Quote Link to comment Share on other sites More sharing options...
rab Posted December 15, 2007 Share Posted December 15, 2007 look at str_replace(). Quote Link to comment 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 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.