helraizer Posted February 5, 2008 Share Posted February 5, 2008 Hi Folks, I have a filtering system to clean up user input in the respect of swearing. The code I have at the moment is this: <?php $dirty = array('rude word', 'another', 'and another rude word', 'another, please Carol'); foreach($dirty AS $bad_word){ $text = preg_replace("/$bad_word/i","****", $text); } ?> So if the user inputted 'rude word me, it works!!!' and then 'and another rude word you' it'd turn into '**** me, it works!!!' and '**** you'. Which makes it look odd, so instead of the preg_replace I was thinking of throwing an error message (which I also have using $errors as an array). <?php define('_SWEAR', 'The word you entered, "'.$word.'", has been detected as being offensive; your post has not been submitted. Sorry for any inconvenience.'); //for the error. ?> How would I get it so that $word is the value in the $dirty array that the user inputted so it'd read: Error! - The word you entered, "rude word", has been detected as being offensive; your post has not been submitted. Sorry for any inconvenience. Otherwise it'd just pull out any if not all of the elements of the array. How would I do this? Thanks, Sam Quote Link to comment https://forums.phpfreaks.com/topic/89453-solved-find-value-in-array-depending-on-word-in-user-input/ Share on other sites More sharing options...
Psycho Posted February 5, 2008 Share Posted February 5, 2008 The code below will only work "well" if there is only 1 bad word in the text. There are different ways to handle if there are multiple bad words and I didn't want to make an assumption. <?php define('_SWEAR', 'The word you entered, [WORD], has been detected as being offensive; your post has not been submitted. Sorry for any inconvenience.'); //for the error. $dirty = array('rude word', 'another', 'and another rude word', 'another, please Carol'); $error = false; foreach($dirty AS $bad_word){ if (strpos($text, $bad_word)) { $error = preg_replace("/[WORD]/", $bad_word, _SWEAR); } } if ($error) { echo $error; } else { //Process the post } ?> Quote Link to comment https://forums.phpfreaks.com/topic/89453-solved-find-value-in-array-depending-on-word-in-user-input/#findComment-458131 Share on other sites More sharing options...
laffin Posted February 5, 2008 Share Posted February 5, 2008 u can use preg_match $errors=array(); foreach($dirty AS $bad_word){ if(preg_match("/$bad_word/i", $text)) $errors[]="$bad_word, is not allowed in this area!"; if(isset($errors)) { foreach($errors as $msg) echo "<H2>$msg</h2><BR>"; die(); } Quote Link to comment https://forums.phpfreaks.com/topic/89453-solved-find-value-in-array-depending-on-word-in-user-input/#findComment-458133 Share on other sites More sharing options...
helraizer Posted February 5, 2008 Author Share Posted February 5, 2008 Thanks guys, it works perfectly now. Sam Quote Link to comment https://forums.phpfreaks.com/topic/89453-solved-find-value-in-array-depending-on-word-in-user-input/#findComment-458156 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.