SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot Tikila's Avatar
    Join Date
    Jun 2007
    Location
    Toronto
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help needed on this spam-filter code,thank you

    PHP Code:
    $badwords = array("rat","badword2","badword3");  foreach($badwords as $word=>$key) {   if(strpos($message,$key) !== false) { echo 'inappropriate content ';   break; } } 
    Now I found out any other word that include "rat" is caught(i.e brat,rattle,etc)

    Can anyone help me modify the code so it ony rejects "rat" and allow "brat" ?
    Cramblibu once helped me with this code but it does not work
    PHP Code:
     $badwords = array("rat","badword2","badword3");  $pattern sprintf('/\b%s\b/i'implode('|'array_map('preg_quote'$badwords))); if (preg_match($pattern$message)) {     // case insensitive badword found } 

  2. #2
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The string tha tyou have in sprinf, should be like this:
    '/\b(%s)\b/i'

  3. #3
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It works:
    PHP Code:
    $message "rat brat badword2 badword3 foo bar";
    $badwords = array( "rat""badword2""badword3" );

    $pattern sprintf'/\b%s\b/i'implode('|'array_map('preg_quote'$badwords)) );
    echo 
    preg_replace$pattern""$message ); 
    my mobile portal
    ghiris.ro

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    krzyk is right, it should be '/\b(%s)\b/i'

    Tikila, why do you massacre the code and put it all one one line?

  5. #5
    SitePoint Zealot Tikila's Avatar
    Join Date
    Jun 2007
    Location
    Toronto
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks everyone but am still having problems with the code

    PHP Code:
    $badwords = array("rat","brat","work from home");
    $pattern sprintf('/\b(%s)\b/i'implode('|'array_map('preg_quote'$badwords)));
     if (
    preg_match($pattern$message)) {     // case insensitive badword found } 
    problem i have is that the code does not work if includes bad words like "</div>","</p>" // the slash part.
    Last edited by Tikila; Apr 5, 2009 at 10:32.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •