SitePoint Sponsor |
|
User Tag List
Results 1 to 5 of 5
Hybrid View
-
Mar 3, 2009, 00:48 #1
- 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; } }
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 }
-
Mar 3, 2009, 02:42 #2
- 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'
-
Mar 3, 2009, 09:53 #3
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
-
Mar 3, 2009, 10:31 #4
- 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?
-
Apr 5, 2009, 07:31 #5
- 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 }
Last edited by Tikila; Apr 5, 2009 at 11:32.
Bookmarks