1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Help with a spam filter (solved)

Discussion in 'PHP' started by qwikad.com, Jan 2, 2018.

  1. #1
    This will flag the words 'betting', 'gambling', 'xxx' but it won't flag 'Betting', 'Gambling', 'XXX'

    I'd like it to flag all variation of those words (i.e. Betting, BEttinG, BETTING, etc. etc.). Obviously, I can list them all separately, but I'd like to match them somehow so that all variations of those words would be flagged.

    
    $original  = $_POST['message'];
    $spamw = array('betting', 'gambling', 'xxx');
    
    foreach ( $spamw as $num ) {
        if ( ( $pos = strpos( $original, $num ) ) !== false ) {
            $err = 'Something went wrong. Please, try again.';
        }
    }
    
    Code (markup):
     
    Solved! View solution.
    Last edited: Jan 2, 2018
    qwikad.com, Jan 2, 2018 IP
  2. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #2
    $original = strtolower($_POST['message']);
    Code (markup):
     
    qwikad.com, Jan 2, 2018 IP
  3. phpmillion

    phpmillion Member

    Messages:
    145
    Likes Received:
    11
    Best Answers:
    4
    Trophy Points:
    45
    #3
    Just a quick note:

    The way you search for banned words will produce false alarms in some scenarios. For example, let's say you add word sex to banned words list. Now Try to submit text "I like loud sexto music" and see what happens - it will be flagged as spam.

    It's just a basic example, but I hope you got the idea. Once you start adding more words to banned words list, the number of false alarms will increase.
     
    phpmillion, Jan 3, 2018 IP
  4. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #4
    So what would you do to make sure that only the selected words are being flagged?
     
    qwikad.com, Jan 3, 2018 IP
  5. phpmillion

    phpmillion Member

    Messages:
    145
    Likes Received:
    11
    Best Answers:
    4
    Trophy Points:
    45
    #5
    You can use preg_match to find blacklisted words. Using this function, you can match whole words, so if some long words contains a part of blacklisted word, no false alarm will be triggered.
     
    phpmillion, Jan 3, 2018 IP
  6. #6
    P.S. Using preg_match, be sure to use /i flag and remove strtolower from your $original = strtolower($_POST['message']); code because this way you overwrite whole message just to scan for blacklisted words. In other words, if user submits a message "Hello, my name is John, I live in New York", you will see it as "hello, my name is john, i live in new york" if you continue using strtolower. However, /i flag in preg_match will allow you to match both UPPERCASE/lowercase words, but will NOT overwrite original message.
     
    phpmillion, Jan 3, 2018 IP
  7. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #7
    With preg_match:

    
    $spamw = array('betting', 'gambling', 'xxx');
    $original = $_POST['message'];
    
    foreach ($spamw as $num) {
      if (preg_match("/\b$num\b/i", $original)) {
        $err = 'Something went wrong. Please, try again.';
      }
    }
    
    Code (markup):
     
    qwikad.com, Jan 4, 2018 IP