Jump to content

Use of strpos and $error_text


hiloes

Recommended Posts

Hey guys  :tease-01:

 

I'm trying to use strpos to stop a form when someone use a word not allowed in the text area and get different message depending of the word.

 

For example Badword1 return Message 1, Badword2 return Message 2..... but..... Instead of that I get all the message one after all...

if (strpos(cP("description"), 'Badword1' , 'Badword2') !== false) {
       $error = true;
        $error_text .= "<div class=\"error-msg2\"><p>Message 1</p></div>";
      }
      if (strpos(cP("description"), 'Badword3' , 'Badword4') !== false) {
       $error = true;
        $error_text .= "<div class=\"error-msg2\"><p>Message 2</p></div>";
      }
         if (strpos(cP("description"), 'Badword5' , 'Badword6') !== false) {
       $error = true;
        $error_text .= "<div class=\"error-msg2\"><p>Message 3</p></div>";
      }

Can you please help me, and maybe find what wrong here  ::)

Link to comment
Share on other sites

Your logic has several NON-nested if statements. Therefore it will evaluate your first search, then your second, then your third without fail. Also - your use of the ".=" to assign the error messages means they will all accumulate. That explains why you have those messages.

 

Question - you are using a syntax for strpos that is not in the manual. The syntax I see is (string,search_string) yet you are using (string,search_string,search_string). I'm surprised it runs at all.

Link to comment
Share on other sites

Besides that, it's a bad idea to copy and paste code sections when you want to repeat a certain action multiple times.

 

As you can see, you really just need a mapping from bad words to error messages and then loop over this mapping:

<?php

const BAD_WORDS = [
    'foo' => "You're not allowed to say 'foo'!",
    'bar' => "You're not allowed to say 'bar'!",
];



$test_text = "This is foobar.";

$error = false;
$error_text = '';
foreach (BAD_WORDS as $word => $message)
{
    if (strpos($test_text, $word) !== false)
    {
        $error = true;
        $error_text .= $message;
        break;
    }
}



if ($error)
{
    echo $error_text;
}
else
{
    echo 'Everything is fine.';
}

Note that “bad word” filters, especially primitive ones like yours, cause more harm than good. Your filter will reject perfectly legitimate input just because happens to share a few characters with an insult (e. g. “assassination”). At the same time, anybody who wants to use insults can of course easily cirumvent the filter.

 

I've actually had this problem on other forums, and I can assure you that it's annoying as hell. I understand that some countries are extremely sensitive about political correctness, but let's not overdo it.

Link to comment
Share on other sites

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.