hiloes Posted January 28, 2016 Share Posted January 28, 2016 Hey guys 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 Quote Link to comment https://forums.phpfreaks.com/topic/300685-use-of-strpos-and-error_text/ Share on other sites More sharing options...
ginerjm Posted January 28, 2016 Share Posted January 28, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/300685-use-of-strpos-and-error_text/#findComment-1530527 Share on other sites More sharing options...
Jacques1 Posted January 28, 2016 Share Posted January 28, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/300685-use-of-strpos-and-error_text/#findComment-1530530 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.