By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,889 Members | 1,044 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,889 IT Pros & Developers. It's quick & easy.

Filtering out Bad Data

P: 9
I have a site written in PHP that allows users to enter info into a form and upload an image. This information gets written to a MYSQL database. My question is how can I filter out dirty words and or pictures from being uploaded into my database ? Is there any simple way to handle this?
Feb 13 '08 #1
Share this Question
Share on Google+
5 Replies


harshmaul
Expert 100+
P: 490
Hi,
The images are going to have to be checked manually. (by yourself or a moderator), and the words can be filtered in a number of ways....

try this one

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $swears = array("swear","bad","naughty");
  3. $strMessage = str_replace($swears, "***", $strMessage);
  4. ?>
and then check this article...

http://uk.php.net/str_replace
Feb 13 '08 #2

Markus
Expert 5K+
P: 6,050
If you want multiple replacements:
[php]
<?php
function filterContents($_str)
{
$_badWords = array("badword1", "badword2", "badword3", "badword4");
$_replace = array("****", "Removed", "OOPS!", "No can do!");

for($_i = 0; $_i < count($_badWords); ++$_i)
{
$_randKey = (rand()%count($_replace)); // random key
$_str = eregi_replace($_badWords[$_i], $_replace[$_randKey], $_str); //replace with a random replacement.
}
return $_str; //return.
}
$_content = "string here.. maybe some submitted comment.. badword1 badword2 badword3..";
echo filterContents($_content);

?>
[/php]
Feb 13 '08 #3

harshmaul
Expert 100+
P: 490
markus, Love the replacements!
Feb 13 '08 #4

Markus
Expert 5K+
P: 6,050
markus, Love the replacements!
You should've seen what i had for the badwords before i realised i better take them out if i wanna remain on thescripts ;)
Feb 13 '08 #5

P: 9
Perfect.
Thanks!
Feb 13 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.