|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
You eat, breathe and sleep innovation. Build your mobile intelligence with BlackBerry® experts this July. Register Today! |
|
#1
|
|||
|
|||
|
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. |
|
#2
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
Thanx!
|
|
#4
|
|||
|
|||
|
Quote:
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. |
|
#5
|
|||
|
|||
|
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. |
|
#6
|
||||
|
||||
|
Quote:
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:
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:
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:
/\b$word\b/i - the i tells it to be case insensative. |
|
#7
|
|||
|
|||
|
Thank you very very much!
![]() |
![]() |
| Viewing: Dev Articles Community Forums > Programming > General Programming Help > bad language screener |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|