Gossamer Forum
Home : General : Perl Programming :

Swear Filter.....

Quote Reply
Swear Filter.....
Hi,

Can someone tell me how to split a form field into individual words.

I have this so far...

$comments = $FORM{Comments};

split(/\s/,$comments);

So the split version of $comments is then in $_ isn't it? - So how do I get each individual word?

Basically I am trying to filter swear words - but I don't think this will work...

if ($_ eq "SWEARWORD") {
$_ =~ /SWEARWORD/*****/;
}

Would it be easier to use something like....

if ($comments =~ /\sWORD/i) {
# Strip the word.....
# But again that doesnt check very well - help pleeeeese
}

Paul Wilson.
http://www.wiredon.net/gt/
http://www.perlmad.com/
Quote Reply
Re: Swear Filter..... In reply to
No, the split is in @_ not $_. You would have to loop through that array and look at each word.

What you could do is, keep an array of all the words you want to block, then a substituition could get it done.

Please note, this is a very quick example, and im sure that given a little thought there would be a better, more efficient solution, but this is just one way to do it, to get you started.

Note this will only work on exact matches, and not with words embedded within another word

Code:
@swears = qw(blah foo sucks);
$comments = 'This is a tame set of foo words. This comment sucks, but hey...blah.';

print "$comments\n";
$comments =~ s/\b$_\b/'*' x length $_/egi foreach @swears;
print "$comments\n";

Installation support is provided via ICQ at UIN# 53788453. I will only respond on that number.
Quote Reply
Re: Swear Filter..... In reply to
Thankyou Mark - the code example is a little over my head but I get the general idea of what it is doing. I will have a go at customizing it -- eeek.

Thanks again.





Paul Wilson.
http://www.wiredon.net/gt/
http://www.perlmad.com/
Quote Reply
Re: Swear Filter..... In reply to
Why don't you had a look at the GT Resources section first Wink
JPDENI wrote a great dirty word sensor. It looks like:

# Censor dirty words
# 0 = do not censor
# 1 = reject records that contain any of the words
# 2 = replace words with ***
# 3 = replace the middle letters with *s, as in "f**k" "a*****e" "c********r"
# 4 = replace vowels with *s, as in "f*ck" "*ssh*l*" "c*cks*ck*r"
# 5 = replace words with the first letter of the word and * for the other letters, as in f*** a******, c*********
# 6 = replace each letter with a *, as in **** ******* **********
$db_censor = 1;

## You can edit the @dirty_words list however you want. Just be sure to put
## single quotes around each word, keep the list all on one line, and be sure they
## are in lower case.
# Dirty word list
@dirty_words = ('[bleep!]','[bleep!]','cocksucker','[bleep!]','cunt','pussy','tits','bastard','bitch','piss');

You can find more @ http://www.jpdeni.com/dbman/Mods/censor.txt

Quote Reply
Re: Swear Filter..... In reply to
....because I didn't expect there to be such a script in the resources section.


Thanks for letting me know.

Paul Wilson.
http://www.wiredon.net/gt/
http://www.perlmad.com/
Quote Reply
Re: Swear Filter..... In reply to
I hope it's usefull for you.

...and just post an URL here if you need a tester! Smile

Quote Reply
Re: Swear Filter..... In reply to
LOL....thanks.....

I ended up not using JPDeni's code because it is for DBMAN so it was easier to use Mark's (it was for a guestbook).

Give it a try if you are feeling lucky...punk!

http://www.perlmad.com/gb/gb.cgi

You will have a tough time getting a swear word to show coz I think I nipped them all in the bud Smile

Give it a try anyway!

Paul Wilson.
http://www.wiredon.net/gt/
http://www.perlmad.com/
Quote Reply
Re: Swear Filter..... In reply to
You forgot regexp for spaces between letters that form "swear" words...also, did you apply the codes to check all columns/fields?

Because I was able to add a swear word in the Web address field.

Regards,

Eliot Lee
Quote Reply
Re: Swear Filter..... In reply to
Hi,

No it is just checking the comments field at the mo.

I will change it. Wink

Thanks :)

Paul Wilson.
http://www.wiredon.net/gt/
http://www.perlmad.com/
Quote Reply
Re: Swear Filter..... In reply to
Hokay - it checks all fields now so if you manage to submit a swear word it's because I forgot to add it to the list - Im not down with the US slang....lol


Paul Wilson.
http://www.wiredon.net/gt/
http://www.perlmad.com/
Quote Reply
Re: Swear Filter..... In reply to
But I don't think people will put swearwords with spaces between. So don't worry :-)

Offcourse you could tell the check-routine remove all spaced before 'checking'; but the people could put down f-u-c-k-y-o-u :-)

Note: I could not post this message on your questbook due too max. 1 entry a person; cookie?


Quote Reply
Re: Swear Filter..... In reply to
Hi,

You might not have wanted to actually type f-u-c........etc Smile

Secondly - yes it uses cookies to stop duplicates but it is only a simple solution but it is annoying enough to stop it happening - people would have to clear their cookies and re-open their browser so most wouldn't bother.

Not a bad guestbook for 30 mins work in my opinion.

Now I gotta get it to span page :)

Paul Wilson.
http://www.wiredon.net/gt/
http://www.perlmad.com/
Quote Reply
Re: Swear Filter..... In reply to
lolol......I just read all your kind comments in my guestbook.

I am going to make it impossible for you to do that!....hehe

Paul Wilson.
http://www.wiredon.net/gt/
http://www.perlmad.com/
Quote Reply
Re: Swear Filter..... In reply to
Maybe you could allow people to put max. 2 comments. One is a little bit un-handy. Because I wanted to put something exstra but couldn't.

Quote Reply
Re: Swear Filter..... In reply to
Hi, if you've got some free time - please try to add some swear words again - thanks.

I have turned off the cookie checking so you can submit away - thanks :)

Paul Wilson.
http://www.wiredon.net/gt/
http://www.perlmad.com/
Quote Reply
Re: Swear Filter..... In reply to
Did I succeed in putting a swear word? I got this (watch the exit;)

<html>
<head>
<title>PerlMad</title>
<meta name="description" content="PerlMad is a perl/cgi script directory covering all genres of scripts.">
<meta name="keywords" content="perlmad, perl, cgi, script, directory, links, resources">

</head>
<body topmargin="0">
<table width="100%" cellspacing="0" cellpadding="0">
<tr>
<td><img src="/title.gif">
</td>
<td align="right">
<script language="javascript">

</script>
<noscript>
<a href="http://www.perlmad.com/cgi-bin/bm/adclick.cgi?manager=adcycle.com&gid=1&id=968" target="_top"><img src="http://www.perlmad.com/cgi-bin/bm/adcycle.cgi?gid=1&id=968" width=468 height=60 border=1 ALT="Click to Visit"></a>
</noscript></td>
</tr>

<tr>
<td bgcolor="#efefef" align="right" colspan="2">
<font size="1" face="verdana"><b>Welcome on Thu Mar 22 20:08:02 2001</b></font>
</td></tr>
</table>

<table width="100%" cellspacing="0" cellpadding="0" height="100%">
<tr>
<form action="/cgi-bin/search.cgi" method="POST">
<td bgcolor="#efefef" height="330" width="20%" valign="top">
<input type="text" name="query" size="14"><input type="submit" value="Go!"><BR><BR>
<li><a href="http://www.perlmad.com/"><font face="verdana" size="2">Home Page</a>


<li><a href="http://www.perlmad.com/cgi-bin/add.cgi"><font face="verdana" size="2">Add URL</a>


<li><a href="http://www.perlmad.com/New"><font face="verdana" size="2">What's New?</a>


<li><a href="http://www.perlmad.com/Cool"><font face="verdana" size="2">What's Cool?</a>


<li><a href="http://www.perlmad.com/Ratings"><font face="verdana" size="2">Top Rated</a>


<li><a href="http://www.perlmad.com/cgi-bin/jump.cgi?ID=random"><font face="verdana" size="2">Random Link</a>


<li><a href="http://www.perlmad.com/cgi-bin/search.cgi"><font face="verdana" size="2">Advanced Search</a>
<BR><BR><BR><center><a href="http://www.wiredon.net"><img src="/wired1.gif" border="0"></a></center>

</td>
</form>
<td valign="top" bgcolor="#ffffff" align="left">
<img src="/gb.gif">
<BR>
<font size="2" face="verdana">
<b>Thankyou for taking the time to use the PerlMad guestbook!</b>
<BR>
Below are our visitors comments:<BR><BR>
<hr color="#efefef" width="80%">

<table><tr><td width="15%"><font size="2" face="verdana"><b>Name:</b></font></td><td><font size="2" face="verdana">Paul Wilson</font></td></tr>
<tr><td><font size="2" face="verdana"><b>Website Name:</b></font></td><td><font size="2" face="verdana">PerlMad</font></td></tr>
<tr><td><font size="2" face="verdana"><b>Website URL:</b></font></td><td><font size="2" face="verdana"><a href="http://www.perlmad.com">http://www.perlmad.com</a></font></td></tr>
<tr><td valign="top"><font size="2" face="verdana"><b>Comments:</b></font></td><td><font size="2" face="verdana">Hi, just a quick note to say we hope you like the new guestbook. You can post your comments about our site and services here - please be nice :)</font></td></tr>
<tr><td><font size="2" face="verdana"><b>Submitted:</b></font></td><td><font size="2" face="verdana">Thu Mar 22 09:31:45 2001</font></td></tr>
<tr><td><font size="2" face="verdana"><b>Email:</b></font></td><td><a href="mailto:perlmad@perlmad.com?subject=I found your email address at PerlMad!"><font size="2" face="verdana">perlmad@perlmad.com</font></a></td></tr></table><hr color="#efefef" width="80%">
<table><tr><td width="15%"><font size="2" face="verdana"><b>Name:</b></font></td><td><font size="2" face="verdana">chrishintz</font></td></tr>
<tr><td><font size="2" face="verdana"><b>Website Name:</b></font></td><td><font size="2" face="verdana">Test.com</font></td></tr>
<tr><td><font size="2" face="verdana"><b>Website URL:</b></font></td><td><font size="2" face="verdana"><a href="http://www.*******.com">http://www.*******.com</a></font></td></tr>
<tr><td valign="top"><font size="2" face="verdana"><b>Comments:</b></font></td><td><font size="2" face="verdana">Just a test!</font></td></tr>
<tr><td><font size="2" face="verdana"><b>Submitted:</b></font></td><td><font size="2" face="verdana">Thu Mar 22 20:08:02 2001</font></td></tr>
</table><hr color="#efefef" width="80%">

</td></tr></table>
</body>
</html>
exit;

Quote Reply
Re: Swear Filter..... In reply to
Hi,

I spotted the exit (must have been just after you) :)

So I take it my filter works ?

Paul Wilson.
http://www.wiredon.net/gt/
http://www.perlmad.com/
Quote Reply
Re: Swear Filter..... In reply to
Nope....Sorry!

Things like: ss-----ww----eeeee-----aa----rr----www----oo----rr---ddd will ALWAYS be possible!


Quote Reply
Re: Swear Filter..... In reply to
Ummm no they wont because my code strips out _ and - .......obviously I cant do that on URL and email fields because some URL's and email addresses use _ and - ....what kind of person is going to bother doing

S-------W---------E---------A-------R

......that doesnt bother me because it is so unlikely that anyone is going to type that.

Paul Wilson.
http://www.wiredon.net/gt/
http://www.perlmad.com/
Quote Reply
Re: [Paul] Swear Filter..... In reply to
Paul,

I can not get either link in your post to work... please tell me where I can find this plug-in.
Quote Reply
Re: [shiner] Swear Filter..... In reply to
Eww this thread makes me cringe looking back a couple of years Wink