SunQuest
 
           General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
You eat, breathe and sleep innovation. Build your mobile intelligence with BlackBerry® experts this July. Register Today!
  #1  
Old December 20th, 2002, 05:18 AM
lord_gino lord_gino is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 5 lord_gino User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question bad language screener

Hello,
I've developed a forums board using PHP and MySql, and I would like to add a bad language screener to it, meaning, every written messaged is scanned, and if it contains a word from a list of forbidden words the message is not to be posted.
Maybe one of you wrote something like this and can give me tips?
Thanx,
Gino.

Reply With Quote
  #2  
Old December 20th, 2002, 07:28 AM
serg4444 serg4444 is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2002
Posts: 16 serg4444 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
1. Create bad words table
2. When somebody add message use eregi_replace function - all words one by one

Sincerely,
Sergey Booyny
AlarIT programmer
http://www.AlarIT.com

Reply With Quote
  #3  
Old December 20th, 2002, 07:55 AM
lord_gino lord_gino is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 5 lord_gino User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanx!

Reply With Quote
  #4  
Old December 20th, 2002, 06:58 PM
jpenn jpenn is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Washington, DC
Posts: 317 jpenn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 3 sec
Reputation Power: 6
Quote:
When somebody add message use eregi_replace function - all words one by one

eregi_replace() is inefficient for this. Use preg_replace() instead - much faster. Here is your code ->

Code:
/*************************************
Function to generate a random muff string
*************************************/ 
function create_string( $length ) {
        mt_srand( ( double )microtime() * 1000000 );
        $possible = '!@#$%^&*-';
        $replace = false;
        while( strlen( $replace ) < $length ) {
                $replace .= substr( $possible, mt_rand( 0, strlen( $possible ) -1 ), 1 );
        }
        return( $replace );
}
/*************************************
The code to do the replacment
*************************************/ 
$string = $your_data_with_words;

foreach ( $your_array_of_words as $val )
	{
	$string = preg_replace( "/\b$word\b/i", create_string( strlen( $val ) ), $string );
}

echo $string;

/*************************************
$your_data_with_words -> The scalar with the data
$your_array_of_words -> The array of bad words to filter from your DB
*************************************/ 
__________________
~ Joe Penn

We work for free to help make this a valuable resource on the internet. Do you appreciate the help - did we provide help that will help you prosper and help that has contributed to sharpening your current skill set?

Show your appreciation and purchase something from our Amazon Wishlist's - it's simple and a great way to say thank you.




Reply With Quote
  #5  
Old December 21st, 2002, 01:49 AM
lord_gino lord_gino is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 5 lord_gino User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thank you very much for your detailed answer.
How do I keep my array of bad words? Just loop it from the DB or something else?
is preg_replace not case sensitive?

Last edited by lord_gino : December 21st, 2002 at 02:22 AM.

Reply With Quote
  #6  
Old December 21st, 2002, 10:44 AM
jpenn jpenn is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Washington, DC
Posts: 317 jpenn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 3 sec
Reputation Power: 6
Quote:
How do I keep my array of bad words? Just loop it from the DB or something else?

Ok, you can keep your words in a DB or even a flat file. If you have a field in your DB with the words, you can use a while() to fetch the array, as so ->
PHP Code:
 $query mysql_query/* YOUR STATEMENTS HERE */ );
while ( 
$array mysql_fetch_array$query ) ) {

   
/* The Replacement code in here */



With the above, you will replace the foreach() in the original code chunk I posted with the above. So you would put the preg_replace() code where the note is above in the while loop.

Now, you can have the words in a flat file also. The file layout would like like this ->
Code:
badword
verybadword
word
thisbadword

To get your array out of the file, use this ->
PHP Code:
 $array_of_words file'path/file.txt' ); 

That will create the array of words for you. Then you would use the foreach() loop as posted in the original chunk of code I posted.

Quote:
is preg_replace not case sensitive?

/\b$word\b/i - the i tells it to be case insensative.

Reply With Quote
  #7  
Old December 21st, 2002, 11:19 AM
lord_gino lord_gino is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 5 lord_gino User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thank you very very much!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > bad language screener


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 

Iron Speed




© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway