What best practices should be followed when modifying a script to include a word censor function for user-generated content?
When modifying a script to include a word censor function for user-generated content, it is important to create an array of banned words and then use PHP functions like str_ireplace to replace those words with a placeholder like "***". This helps maintain a clean and respectful environment for all users.
<?php
// Array of banned words
$banned_words = array("badword1", "badword2", "badword3");
// User-generated content
$user_content = "This is a badword1 example of badword2 user-generated badword3 content.";
// Replace banned words with ***
$censored_content = str_ireplace($banned_words, "***", $user_content);
// Output censored content
echo $censored_content;
?>
Related Questions
- How can UTF-8 encoding impact the display of graphics generated in PHP?
- Are there specific configurations or dependencies that need to be checked or adjusted in order for phpMyAdmin to function properly in a PHP environment?
- What are some common mistakes made by PHP beginners when working with date and time functions?