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;
?>
Related Questions
- In what situations would it be appropriate to move a PHP-related issue to an HTML board for further discussion?
- What are the advantages and disadvantages of using $_SERVER["HTTP_REFERER"] to determine parameter values in PHP scripts?
- What are some potential connecting problems with MySQL that could cause issues like constant page reloading or display errors in PHP forums?