0

I want to check if the content from a text area contains certain "bad words". I read the content from the textarea with $_POST['message'] This is my textarea:

<textarea class="form-control" name="message" placeholder="MESSAGE"></textarea>                             

All the bad words are in an array:

// read bad words into array
$blacklistfile = 'blacklist/badwords.txt';
$blacklistarray = file($blacklistfile, FILE_IGNORE_NEW_LINES);

To check if the content of $_POST['message'] contains any of these bad words, i thought: put all the content of $_POST['message'] into an array and compare these arrays.

I tried in the textarea [] after the message:

<textarea class="form-control" name="message[]" placeholder="MESSAGE"></textarea>   

and:

$usermessage = $_POST['message'];
print_r($usermessage);

But print_r() gives me no output

So how can i put the content of the textarea into an array??

Or: maybe there are other possibilities to achieve this?

4
  • Your textarea only sends one single string value. What do you hope to achieve, by wrapping that in an array? You would still have an array consisting of only one element then. Commented Feb 27, 2020 at 12:02
  • You should loop over your bad words, and then check if they are contained in the value you got from the text area. Commented Feb 27, 2020 at 12:03
  • (How rather pointless such a naive approach eventually is though, is clear right from the beginning, I hope? If you don’t let me post ass, then I will try a.s.s. or a-s-s instead.) Commented Feb 27, 2020 at 12:04
  • There are a lot of ways you could filter the message as a string instead of an array as well. Like @CBroe says, it all depends on what's acceptable and what isn't. is a-s-s acceptable? if everything besides the full word(so without spaces next to eachother) is acceptable You can just post the message as string and use the php function explode and explode on a space. Or even another way. There's a lot of ways of achieving this. Commented Feb 27, 2020 at 12:11

1 Answer 1

2

Your textarea will submit one string

<textarea class="form-control" name="message" placeholder="MESSAGE"></textarea>

On server you must explode this string on spaces to get array of words

$textareaValue = $_POST['message'];
$wordsToCheck = explode(" ",$textareaValue);

Also you can filter unique words to avoid duplicate iteration

$wordsToCheck = array_unique($wordsToCheck); 

After that, you can compare each textarea words to badlist

foreach ($wordsToCheck as $word){
    if(in_array($word, $blacklistarray)){
        //word is bad
    }else{
        //it's ok
    }
}

Did I understood your question correctly?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.