What are the best practices for filtering out bad words from a text using PHP?

Filtering out bad words from a text using PHP involves creating a list of inappropriate words and then comparing each word in the text against this list. If a match is found, the word can be replaced or removed. Regular expressions can also be used for more complex filtering.

$badWords = array("badword1", "badword2", "badword3");
$text = "This is a text with badword1 and badword2.";

foreach($badWords as $badWord){
    $text = str_ireplace($badWord, "***", $text);
}

echo $text;