How can one efficiently detect and handle bad words in a text using PHP without regular expressions?
Detecting and handling bad words in a text using PHP without regular expressions can be achieved by utilizing the str_ireplace function to replace each bad word with a predefined "clean" word. By creating an array of bad words and their corresponding clean words, we can efficiently filter out inappropriate language from the text.
<?php
function filterBadWords($text) {
$badWords = array("badword1" => "cleanword1", "badword2" => "cleanword2");
foreach($badWords as $badWord => $cleanWord) {
$text = str_ireplace($badWord, $cleanWord, $text);
}
return $text;
}
$text = "This is a text with badword1 and badword2.";
$filteredText = filterBadWords($text);
echo $filteredText;
?>