How can PHP developers efficiently implement word filtering functionality in their applications to prevent spam and inappropriate content?
To efficiently implement word filtering functionality in PHP applications to prevent spam and inappropriate content, developers can use a combination of regular expressions and arrays to match and replace prohibited words with a replacement string.
function filter_words($input, $prohibited_words){
$filtered_input = $input;
foreach($prohibited_words as $word){
$filtered_input = preg_replace("/\b$word\b/i", "[filtered]", $filtered_input);
}
return $filtered_input;
}
$input_text = "This is a sample text with inappropriate words like bad and rude.";
$prohibited_words = array("bad", "rude", "inappropriate");
$filtered_text = filter_words($input_text, $prohibited_words);
echo $filtered_text;
Related Questions
- What are some best practices for handling email sending in PHP to avoid errors?
- What are best practices for mixing procedural and object-oriented styles in PHP code to avoid potential conflicts or errors?
- How can PHP be used to query a MySQL database for user information and display it on a dynamically generated page?