-2

SOLVED: Using Answer from Michael Thompson and "Duplicate" Question Users Flagged

All of the code used below is in PHP!

This is not the same as other questions asked on Stack Overflow I have tried the examples people have linked and nothing will work for me!

Prior to Asking on StackOverflow

I've googled this quite a few times, and tried many different tutorials, all of them being different from the other and making it more confusing. Using code like preg_match(), strpos() etc. and can't get it quite right.

Main Question:

I have a registration form on my site. For example, if the user were to sign up with a username like Bad or BadWord, I want it to stop them from doing so.

I have an array of bad words:

$badWords = array('some', 'bad', 'words');

My username variable is:

$username

I want to do a check, for example, if Bad or BadWords contains a word from the array, if it does then it will echo 'bad word detected!'

Pseudo code for example:

if ($username contains $badWords) {
     echo 'bad word detected!';
}

I don't have the greatest knowledge of PHP as I only began to learn it a few months ago, that is why I am asking for help.

Thanks!

  • `if (in_array($username,$badWords))` – Shadow Fiend Jan 09 '18 at 01:12
  • 4
    You found it necessary to make this post NSFW by actually listing profanity in it, while asking a question about not allowing it? This is a professional, family friendly site. Please treat it as such. And what about names that validly claim some part of your list, such as NFL football legend Eric **Dick**erson? Or sportscaster **Dick** Engberg? – Ken White Jan 09 '18 at 01:12
  • @ShadowFiend tried that already, didn't work – SimmsRyan97 Jan 09 '18 at 01:13
  • 3
    Why do you want to do this? BTW Dick is a shorten version of Richard – Ed Heal Jan 09 '18 at 01:13
  • @KenWhite Sorry I didn't realise – SimmsRyan97 Jan 09 '18 at 01:14
  • you sure? Because I've been using that code times and its working.. – Shadow Fiend Jan 09 '18 at 01:14
  • Why recreate the wheel? Google it: https://www.google.com/search?q=php+curse+word+detector&oq=php+curse+word+detector&aqs=chrome..69i57.2733j0j7&sourceid=chrome&ie=UTF-8 , also read https://stackoverflow.com/questions/273516/how-do-you-implement-a-good-profanity-filter – pepperjack Jan 09 '18 at 01:15
  • @pepperjack Because there's value in developing something yourself and learn in the process. Especially for beginners. – icecub Jan 09 '18 at 01:16
  • @pepperjack Already read it, I've read countless articles – SimmsRyan97 Jan 09 '18 at 01:16
  • 1
    @icecub: Except the poster is not *developing something themself*. They're asking us to do it. – Ken White Jan 09 '18 at 01:17
  • @SimmsRyan97 A basic profanity filter process would be an array of curse words (like you have), then decapitalize the terms and the string in question, and for each term search the string for that term to see if it is present... what part of this process are you having issues with? Some helpful links for learning stuff required for this: http://php.net/manual/en/function.strpos.php http://php.net/manual/en/control-structures.foreach.php http://php.net/manual/en/function.strtolower.php – pepperjack Jan 09 '18 at 01:18
  • @KenWhite True. Though sometimes if you don't know where to start, a little example can help get you going. I know it's a bad question, but I'm trying to look at it from OP's point of view as well. Hence if the question is simple and small enough to be answered in a comment, I don't mind helping them out. – icecub Jan 09 '18 at 01:21
  • @ShadowFiend works for a single word but if it's like BadWord then it won't be detected – SimmsRyan97 Jan 09 '18 at 01:23
  • 1
    https://en.wikipedia.org/wiki/Dick_Butkus – Sammitch Jan 09 '18 at 01:35

1 Answers1

1

The simplest way would be to loop through the array with a for loop, and then check if each index exists inside the string. That can be achieved like so:

$badWords = array('fk', 'st', 'ct', 'bd', 'dk');
$containsBadWord = false;
foreach ( $badWords as $badWord ) {
    if ( stripos($username, $badWord) ) {
        $containsBadWord = true;
        break; //We do this just to save a few loops where unneccesary
    }
}
if ( $containsBadWord ) {
    echo 'That username contains a word or words that are undesirable. Please pick a different username.';
} else {
    echo 'That username does not contain a word or words that are undesirable, and you can use that.';
    //Provided that the username passes other checks such as whether it has not been used before
}

I would suggest using stripos in this context instead of strpos, due to the case insensitive nature of stripos. Also, if you do not understand the foreach, click the link to learn more about it on the PHP website. The foreach behaves the same here as a for loop as below:

$count = count($badWords);
for ( $i=0; $i<$count; $i++ ) {
    if ( stripos($username, $badWords[$i]) ) {
        $containsBadWord = true;
        break; //We do this just to save a few loops where unneccesary
    }
}
Michael Thompson
  • 541
  • 4
  • 21
  • I have just redacted those words. Thanks. – Michael Thompson Jan 09 '18 at 01:24
  • 1
    You didn't see the comments above about how posting a list of profanity isn't conducive to keeping this a professional, family friendly site that is safe to use at work? And you didn't *redact* anything; they're still there, in the first line of code in your first codeblock. – Ken White Jan 09 '18 at 01:25
  • I have now seen those comments, which is the reason why I removed the words. Yes, I have left the first and last characters of each word, no, they are no longer profanity. I have never seen "fk" listed as profanity. Feel free to correct this as you see fit. Also, I should mention I meant retracted not redacted. – Michael Thompson Jan 09 '18 at 01:29
  • I tried either one, didn't work – SimmsRyan97 Jan 09 '18 at 01:41
  • did you get any errors or feedback in any way? – Michael Thompson Jan 09 '18 at 01:43
  • @MichaelThompson I combined your answer with the post people flagged mine as a duplicate of and it worked. I added !== false in the first if statement – SimmsRyan97 Jan 09 '18 at 02:09
  • Excellent. Now typically, you should not need to add !== false, as if it is not false then it is true and PHP will continue execution. The only time you would wish to use this is when you want PHP to consider 0 as true, which it would otherwise consider as false. Given that PHP's stripos may return 0 if the string is found a position 0, you were correct to add that and it was an oversight of mine not to do so, so well done! – Michael Thompson Jan 09 '18 at 02:57
  • This will fix the code ( stripos($username, $badWord) !== false ) – unobatbayar Nov 21 '21 at 11:18