Jump to content

Badword filtering slow


XtremeX

Recommended Posts

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

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