Check for forbidden words from array

Views: 36 Last modified: August 03rd, 2011 Comments: 0

This function will check in a string for words which are not allowed, defined in an array. Function is case-insensitive. So you are allowed to use only upper or lowercase letters.

function show_invalid_words($text, $array)
{
	for ($i = 0; $i < count($array); $i++)
	{
		if (preg_match("/$array[$i]/i", $text))
			return false;
	}

	return true;
}

Exmaple

$bad_words = array('this', 'andX');
echo (int)show_invalid_words('HELLO! This is jusT a phrase. Simple And well', $bad_words);

The above will out 0 as one of the words are found.

VN:F [1.9.13_1145]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.13_1145]
Rating: 0 (from 0 votes)

    Mail this!

    To: From:Sum {0+2} =  
    Anything to add ?

        You must be logged in to post a comment.