What are the benefits of creating your own bad word filter in PHP?

Creating your own bad word filter in PHP allows you to control which words are considered inappropriate or offensive in your application. This can help maintain a positive user experience, prevent inappropriate content from being displayed, and comply with community guidelines or regulations. By implementing a custom bad word filter, you can easily update and customize the list of banned words to suit your specific needs.

function filterBadWords($text, $badWords) {
    foreach($badWords as $badWord) {
        $replace = str_repeat('*', strlen($badWord));
        $text = preg_replace("/\b$badWord\b/i", $replace, $text);
    }
    return $text;
}

$badWords = ['bad', 'offensive', 'inappropriate'];
$text = "This is a bad example of offensive language.";
$filteredText = filterBadWords($text, $badWords);

echo $filteredText;