How can PHP developers efficiently implement word filtering functionality in their applications to prevent spam and inappropriate content?

To efficiently implement word filtering functionality in PHP applications to prevent spam and inappropriate content, developers can use a combination of regular expressions and arrays to match and replace prohibited words with a replacement string.

function filter_words($input, $prohibited_words){
    $filtered_input = $input;
    foreach($prohibited_words as $word){
        $filtered_input = preg_replace("/\b$word\b/i", "[filtered]", $filtered_input);
    }
    return $filtered_input;
}

$input_text = "This is a sample text with inappropriate words like bad and rude.";
$prohibited_words = array("bad", "rude", "inappropriate");

$filtered_text = filter_words($input_text, $prohibited_words);

echo $filtered_text;