So I get how to replace certain words with other ones. What I'm trying to figure out is how to take a word and replace it with a phrase and eliminate all other input.
For example:
bad word is 'dog'
user inputs -> 'You smell like a dog.'
instead of it replacing 'dog' with 'rainbow' or something, I want it to echo something like: 'You are a potty mouth'.
Here's what I have for code:
<?php
$find = array('dog', 'cat', 'bird');
$replace = 'You are a potty mouth.';
if (isset ($_POST['user_input'])&&!empty($_POST['user_input'])) {
$user_input = $_POST['user_input'];
$user_input_new = str_ireplace($find, $replace, $user_input);
echo $user_input_new;
}
?>
With this code it echos: 'You smell like a You are a pottymouth.'
I'm sure this is a repost and I apologize. Everything I've been able to find is documentation on how to replace only parts of strings, not entire ones.
Well, in this case you can just check whether there is a "bad word" in the user input string, and if it returns true, echo "You are a potty mouth."
You would want to use strpos()
e.g.
if( strpos($_POST['user_input'],'dog')!==FALSE ) {
echo('You are a potty mouth');
}
If you have an array of "bad words" you'll want to loop through them to check any occur within user input.