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
https://forums.phpfreaks.com/topic/81808-badword-filtering-slow/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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