What are some best practices for implementing a "Badwords-Funktion" in PHP using string replacement functions?

Implementing a "Badwords-Funktion" in PHP involves creating a function that checks a given string for any inappropriate words and replaces them with a specified replacement string. This can be achieved using string replacement functions like str_replace or preg_replace.

function filterBadwords($inputString, $badwords, $replacement = '***') {
    return str_ireplace($badwords, $replacement, $inputString);
}

$inputString = "This is a badwords example with some inappropriate words.";
$badwords = ['badwords', 'inappropriate'];

echo filterBadwords($inputString, $badwords);