I have an email form on my site which has an action to a php page. The php page detects whether the user has entered a name and email, if they haven't it directs them back, if they have it sends an email to me. I want to add a php cussword filter that would prevent the message from being sent if the message contains any words that i want to block out. Does anyone know how i can accomplish this?

This is a bit rough, there's probably a better way. But this works.

// Array of blocked words
$bad_words[0] = "badword1";
$bad_words[1] = "badword2";
$bad_words[2] = "badword3";
	
foreach($bad_words as $word) {
	if(strpos(strtolower($email_message), $word) !== false) {
	      // Set flag to check when you exit the loop
              $message_ok = false;
	}
}

if(!$message_ok) {
    // Some code to be executed if a vulgar word is found
}
Member Avatar for diafol

If the email is only going to you - what's the problem? Spam? If you're upset by profanity, I bet it's gonna upset the f*** out of you to type in those words which you wanna filter out in the first place.

IF you're worried about automated spam, put a captcha field in your form. Also, have a timeout feature that only allows one post per 15 minutes from a particular IP address. Coz I could send b0ll0ck5 to you, you BUGG3R, and you probably won't pick it up. (Sorry no offence!)

Having the filter won't hurt, but it won't stop all of the brown stuff slopping through the mailbox.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.