How can a word censor function be implemented in PHP to filter out bad words in a text editor?

To implement a word censor function in PHP to filter out bad words in a text editor, you can create an array of bad words and then use the str_ireplace() function to replace those bad words with asterisks or any other desired replacement. This function will search for bad words case-insensitively and replace them with the specified replacement.

function censorBadWords($text){
    $badWords = array("badword1", "badword2", "badword3");
    foreach($badWords as $word){
        $text = str_ireplace($word, str_repeat("*", strlen($word)), $text);
    }
    return $text;
}

$text = "This is a text with badword1 and badword2.";
$censoredText = censorBadWords($text);
echo $censoredText;