What is the best way to prevent certain words from being used in a message and display a custom message instead in PHP?
To prevent certain words from being used in a message and display a custom message instead in PHP, you can create an array of forbidden words, check if any of those words are present in the message, and if so, display a custom message instead. This can be achieved using functions like `str_replace` or `preg_replace` to replace the forbidden words with the custom message.
<?php
$message = "This is a message containing a forbidden word.";
$forbiddenWords = array("forbidden", "word");
foreach ($forbiddenWords as $word) {
if (stripos($message, $word) !== false) {
$message = str_ireplace($word, "****", $message);
}
}
echo $message;
?>