This is probably v.easy but my php is very rusty. I need to implement a bad word filter...I currently have this:
if(!in_array($_POST['Name'],$profanities)) { echo $row['Name']; }
...and an profanities array with bad words in it. Problem is I want to be able to check if any portion of the name the user has entered contains one of those bad words....what I've currently got only checks if the name is the exact bad word....how do i do this?
thanks
$name = $_POST['Name'];
foreach($profanities as $profanity) {
if (preg_match('/\w' . preg_quote($profanity, '/') . '\w/i', $name) {
// Bad word on its own
}
}
This will match a bad word on its own only... i.e. it will match bad
in Is it bad?
but not in baddy
. You can remove the word boundaries if you want to match that, or perhaps try strstr()
.
However, a valid name may contain a substring that is considered bad if you decide to check for simple string matching.
What if I wanted to name my username after my hometown?