How can the logic in the rmBadwords function be improved to properly censor bad words in a given string?

The issue with the current rmBadwords function is that it only checks for exact matches of bad words in the input string. To properly censor bad words, we can use regular expressions to match variations of the bad words, such as different spellings or characters. By using regular expressions, we can effectively censor bad words in the given string.

function rmBadwords($string, $badwords) {
    foreach($badwords as $badword) {
        $pattern = '/\b' . preg_quote($badword, '/') . '\b/i';
        $replacement = str_repeat('*', strlen($badword));
        $string = preg_replace($pattern, $replacement, $string);
    }
    return $string;
}

// Example usage
$badwords = ['badword1', 'badword2'];
$inputString = 'This is a badword1 and badword2 example.';
$censoredString = rmBadwords($inputString, $badwords);
echo $censoredString;