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
- How can the GD library be utilized for image resizing in PHP?
- How can the return value of fwrite() be deactivated or hidden in PHP to prevent it from being displayed on the page?
- What are the implications of using different directory separators in HTML for displaying images across different browsers?