Jump to content

[SOLVED] find value in array depending on word in user input


Recommended Posts

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

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

}

?>

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();
}

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.