0

I am trying to find out if the user has entered any inappropriate words in my registration form. I thought I would use the strpos function but not sure if the code is correct or not?

I tried using the strpos function and supplied 3 variables called $first, $find, $offset to find any inappropriate words in the $find variables for the variable $first

$offset = 0;
    $find = array('retard', 'stupid', 'rascist', 'bastard', 'fuck', 'fuck-off');
if (strpos($first, $find, $offset)) {
                     header("Location: ../signup.php?signup2=rudewords");
                    exit();

But I guess the error here is that I do not know how to use the array with the strpos

Updated code:

if (array_sum(array_map(function ($i) use ($first, $last, $email, $uid, $password) {
                   return strpos($first, $last, $email, $uid, $password, $i) !== FALSE;
                    }, $find)) > 0) {
                    header("Location: ../signup.php?signup2=badwords");
                    exit();
piano0011
  • 23
  • 6
  • Here is the answer : https://stackoverflow.com/a/9220624/999617 – Vincent Decaux Jun 08 '19 at 09:34
  • What is the text you are trying to search for these keywords? – Tim Biegeleisen Jun 08 '19 at 09:35
  • Possible duplicate of [Using an array as needles in strpos](https://stackoverflow.com/questions/6284553/using-an-array-as-needles-in-strpos) – Marvin Jun 08 '19 at 09:44
  • I am still confused after looking through the other posts but I am trying to search through my registration form to make sure that no rude word has been entered in my variables called $first, $last, $uid, $password etc – piano0011 Jun 08 '19 at 09:52
  • Please refrain from using abusive keywords in your code. You can safely replace them with `keyword1`, `keyword2`, etc. – double-beep Jun 08 '19 at 11:00

1 Answers1

1

Here's a way you could use to detect bad words in your form input (using your example as a base):

$offset = 0;
$find = array('retard', 'stupid', 'racist', 'bastard', 'fuck', 'fuck-off');
$first = "you're a fuckin' stupid";

if (array_sum(array_map(function ($i) use ($first) {
    return strpos($first, $i) !== FALSE;
}, $find)) > 0) {
    echo "BAD!";
}

If you want this synthesized into a quick function, it could be something like this:

function has_bad_words($input, $badwords) {
    $output = false;

    if (!empty($input)) {
        if (!empty($badwords)) {
            if (is_array($badwords)) {
                $output = array_sum(array_map(function ($i) use ($input) {
                    return strpos($input, $i) !== FALSE;
                }, $badwords)) > 0;
            }
        }
    }

    return $output;
}

There's an even better approach. If you use this library, it will do what you're looking for with support for many languages, you can improve the dictionaries and it's really easy to integrate.

Hope that helps :)

Julio María Meca Hansen
  • 1,303
  • 1
  • 17
  • 37
  • Thanks but why must I use array_sum and array_map instead of strpos? Is it possible to use strpos as well because I am just learning about it at the moment – piano0011 Jun 08 '19 at 10:03
  • Can I also do the following instead? return strpos($first, $last, $password, $i) !== FALSE; – piano0011 Jun 08 '19 at 10:06
  • I see, it is using strpos but I also have to use array_map – piano0011 Jun 08 '19 at 10:08
  • I hope that I can add more than one variables as shown above – piano0011 Jun 08 '19 at 10:08
  • I have another weird problem and when I tried to sign up legitimately, it says that I still have a rude word... – piano0011 Jun 08 '19 at 10:19
  • I guess that I can always do more if statement with that but can I someone how put more than one variables in that code? – piano0011 Jun 08 '19 at 10:27
  • $offset = 0; $find = array('retard', 'stupid', 'racist', 'bastard', 'fuck', 'fuck-off'); $first = "you're a fuckin' stupid"; if (array_sum(array_map(function ($i) use ($first, $last) { return strpos($first, $last,$i) !== FALSE; }, $find)) > 0) { echo "BAD!"; } – piano0011 Jun 08 '19 at 10:28
  • I have got the following code but now it won't let me register – piano0011 Jun 08 '19 at 10:34
  • I got the above updated code but it won't let me register now with the correct credentials – piano0011 Jun 08 '19 at 10:36
  • I got it to work but I had to use that above statement a few times for each of my variable. I am just wondering if there is an easier way to include all variables into a single code? Something like below – piano0011 Jun 08 '19 at 10:56
  • $offset = 0; $find = array('retard', 'stupid', 'racist', 'bastard', 'fuck', 'fuck-off'); $first = "you're a fuckin' stupid"; if (array_sum(array_map(function ($i) use ($first, $last) { return strpos($first, $last, $i) !== FALSE; }, $find)) > 0) { echo "BAD!"; } – piano0011 Jun 08 '19 at 10:57
  • if you use the function I wrote for you it could be something like this: if (array_sum(array_map(function ($i) use ($badwords) { return has_bad_words($i, $badwords); }, $_POST)) > 0) { header("Location: Location: ../signup.php?signup2=badwords"); } – Julio María Meca Hansen Jun 08 '19 at 11:05
  • I haven't learn much about function yet though...... Is there another way to do it or I guess I don't mind using multiple codes – piano0011 Jun 08 '19 at 11:09