You can try this code that has worked on my website. Replace all variables called your variable here with your variable. You need a CSV file that contains the expletives. This code is able to distinguish between a swear word and an inocent word containing a swear word, for example Scunthorpe. It also replaces the word with the appropriate number of stars, and will recognise all common suffixes. It can take a while to run, but significantly reduces the risk of false positives.
//inport profanities csv and list suffixes
$profanities=explode(",", file_get_contents('NAME OF YOUR CSV FILE GOES HERE'));
$suffixes=array('','s','es','e','ed','ing','ted','ting','y','ty','d','head','bag','hole','wit','tard','er','ter','en','ten','est','test','able','ible','ful','full');
//get text input
$sanitize_text=$YOUR VARIABLE HERE;
//combine profanities and sufixes
foreach($profanities as $profanity)
{
foreach($suffixes as $suffix)
{
$sanitize_terms=$profanity;
$sanitize_terms.=$suffix;
$word=$sanitize_terms;
$match_count=preg_match_all('/'.$word.'/i', $YOUR VARIABLE HERE, $matches);
for($i=0; $i<$match_count; $i++)
{
$bwstr=trim($matches[0][$i]);
$sanitize_text=preg_replace('/\b'.$bwstr.'\b/', str_repeat("*", strlen($bwstr)), $sanitize_text);
}
}
}
$YOUR VARIABLE HERE=$sanitize_text;