Вы можете просто подготовить список плохих слов в массив и использовать следующий код для замены таких слов на *.
$badWords = array("badWord1", "badWord2", "badWord3", "badWord4", "badWord1");
$cleanUp = str_replace($badWords, "****", $originalString);
Если вы хотите идентифицировать такие слова, включая их расширения (например, ing, ed, s, ly и т. Д.), И ответить сообщением о том, что оно неуместно, вы можете сделать следующее.Любой, как вы должны получить полный список таких плохих слов, чтобы идентифицировать его.
// Array of Bad words
$words = array('badWord1','badWord2','badWord3','badWord14');
// Array of extention to words
$exten = array('','ed','ing','s');
// Input string
$str = 'this is a dam sentence';
// Create an array from input
$string = explode(' ',strtolower($str));
// Create a new array for all combinations of swear words
$wordList = array();
// Add all combinations to the new array
foreach($words as $word){
foreach($exten as $ext){
$wordList[] = $word.$ext;
}
}
// Loop through each input word, and check if it is a bad word or not
// FALSE = Good Words
// TRUE = Bad Words
$badWord = FALSE;
foreach($string as $s){
if(in_array($s, $wordList)){
$badWord = TRUE;
}
}
// Do something if output is good or bad in this case display a message
if($badWord)
echo 'This input contains inappropriate content';
else
echo 'This is valid input!';