How can PHP be used to implement a word filter to detect inappropriate words within text?
To implement a word filter in PHP to detect inappropriate words within text, you can create an array of inappropriate words and then use PHP's `str_ireplace` function to replace any occurrences of these words with a placeholder like "***". This will help censor the inappropriate words in the text.
<?php
$inappropriate_words = array("badword1", "badword2", "badword3");
$text = "This is a sample text containing badword1 and badword2.";
foreach($inappropriate_words as $word) {
$text = str_ireplace($word, "***", $text);
}
echo $text;
?>
Related Questions
- What are the best practices for handling different versions of a webpage in PHP without using multiple HTML files?
- What considerations should be made when deciding between using sessions or URL parameters for language management in a PHP application?
- What is the significance of the semicolon after the while loop condition in the PHP code provided?