Gossamer Forum
Home : General : Perl Programming :

text check

Quote Reply
text check
making a simple send email script using a simple form and sendmail.

Now, I want to check the text which is being sent for "bad" words. Say I got a file, including the words with separated with a |. I want to check if this text includes one, or more of these words.

How should I do it?
All the words are on the same line. Should I then use
Code:

foreach $line (@file) {
chomp($line);
}

Or? And I must use a regexp to find the words, right?

Code:
foreach $line (@file) {
if ($message =~ /$line/i) {
print "We don't like the words you used in the mail. Therefore we didn't send it! Ha!";
}

}

Something like this? I really stink at using regexps.
Someone got a clue how this should be done?

Thanks.

- perlman
Quote Reply
Re: [perlman] text check In reply to
I would use something like;

Code:
#!/usr/bin/perl

use strict;
print "Content-type: text/html \n\n";

my $string = "just a test string you can put whatever crap yoy want here";

my @bad_words = qw("test","poo","crap","something","word");

foreach my $line (@bad_words) {
if ($string =~ /$line/i) {
print "There was a problem. You has \"$line\" in your string, so its not gonna be sent now...";
exit;
}
}

BTW: The above code is not tested Wink

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] text check In reply to
Okey, thanks!
I'll try it.

But one question: Why do you use "my" all the time?
Ain't "my" to be used to keep variables inside subroutines? I'm not so familiar with the use of my. Haven't used it enough to really learnt all it's ways to be used.

Thanks.

- perlman
Quote Reply
Re: [Andy] text check In reply to
Be careful with that. If a ban word is crap and someone enters "scrap that" then it will block it.
Quote Reply
Re: [perlman] text check In reply to
Quote:
Ain't "my" to be used to keep variables inside subroutines? I'm not so familiar with the use of my. Haven't used it enough to really learnt all it's ways to be used.

Paul got me into it Wink Basically, it helps you when writing code. If a variable is mis-typed (i.e $vsr instead of $var) an error will occur in the script. This saves hours and hours of development time with problems where you are comparing variables for values that are not even held in that variable. Its not fool-proof, but it certainly helps Smile

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [perlman] text check In reply to
In andy's example he doesn't use any blocks {} so yeah you are right he doesn't need to use my() Wink

Search cpan.org for "strict" and you'll find more info on declaring variables and strict coding.
Quote Reply
Re: [Paul] text check In reply to
>>>In andy's example he doesn't use any blocks {} so yeah you are right he doesn't need to use my()
<<<

Erm, so why do you always tell me to do it? Wink Either way, its a good habit to get into, so better to learn on the easier scripts than to keep forgetting :p

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] text check In reply to
Quote:
Erm, so why do you always tell me to do it?

Because it improves your coding. I've never told you to use it in the way you've used it above =)
Quote Reply
Re: [Paul] text check/email check In reply to
Eh, thanks. Still a bit confused about the using of my, but I think I'm getting there. Read what perdoc.com has written about it and that helped me on the way! Wink

While we're in on the topic of checking and regexps.
If I should check if an email address is entered right, is this okey?
Code:
if (!$emailto) {
error("You must write the email address the message should be sent to. Please go back and fill in all the fields. \n");
}
elsif ($emailto !~ /[\w\-]+\@[\w\-]+\.[\w\-]+/) {
error("Error - you didn't enter a valid email-address. Please go back and try again.\n");
}

I haven't tested it, but you wont find much more secure ways to check it, wont you?

And about the 'my' again.
If I should set some variable, could I do it like this:

Code:
my $emailto = $FORM{'emailto'};
my $emailfrom = $FORM{'emailfrom'};
my $namefrom = $FORM{'namefrom'};
my $nameto = $FORM{'nameto'};
my $message = $FORM{'message'};
my $messagelength = length ($$message);

I used them right before the check part, like the one above. Can it be done like this?
Quote Reply
Re: [perlman] text check/email check In reply to
I normally use;

[0-9a-z]([-_.+]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])
*\\.[a-z]{2,}$

I have found that works the best for me.

Re. second question...Yeah, you can do it like that, or you could do something like;

my ($emailto, $emailfrom, $namefrom, $nameto, $message, $messagelength);

Also, why do you use a double $ sign in your code?

my $messagelength = length ($$message);

Maybe just a typo?

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] text check/email check In reply to
That email regex isn't very reliable.
Quote Reply
Re: [Andy] text check/email check In reply to
So you mean that I can use it like this:

Code:
if ($mailto !~ [0-9a-z]([-_.+]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z]) *\\.[a-z]{2,}$ ) {
error("message");
}

?

And how can I just use my to set the variables like you did? Doesn't I have to show that for example $emailto = $FORM{'emailto'}; ??

And the double $ was just a typo! Tongue

Thanks.

- perlman
Quote Reply
Re: [perlman] text check/email check In reply to
if ($mailto !~ /[0-9a-z]([-_.+]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z]) *\\.[a-z]{2,}$/i ) { error("message"); }

....and....

my ($emailto);

$emailto = $FORM{'emailto'};

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] text check/email check In reply to
Thanks Andy!

I think this will do for now! Wink

- perlman
Quote Reply
Re: [perlman] text check/email check In reply to
That regex will reject valid emails, for a looser, but less restrictive regex, use:

\S+@\S+\.\S+
Quote Reply
Re: [Paul] text check/email check In reply to
You sure?

Then I pity Andy who has been using that code and rejected, maybe many emails and such! Cool

I'll try your method Paul and test it to see if it's of any good.

Thanks.

-perlman
Quote Reply
Re: [perlman] text check/email check In reply to
Quote:
You sure?

Yeah.
Quote Reply
Re: [Paul] text check/email check In reply to
Quote:
You sure?

Yeah.

Good.

Thanks.

- perlman
Quote Reply
Re: [Paul] text check/email check In reply to
So why would my regex reject valid emails? Just wanna know, so I can improve it, and make it work with other valid emails :)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] text check/email check In reply to
[Wed May 14 22:49:33 2003] sendmail.pl: Possible attempt to separate words with commas at sendmail.pl line 82.

I got this error on your line Andy:

Code:
my @bad_words = qw("test","poo","crap","something","word");

Ehh...what's wrong?
Quote Reply
Re: [perlman] text check/email check In reply to
The syntax is wrong, use:

Code:
my @bad_words = ("test","poo","crap","something","word");

or...

Code:
my @bad_words = qw(test poo crap something word);
Quote Reply
Re: [perlman] text check In reply to
its not tested, Try it.

my $input_message = "whatever_it_is";

my $response = "No bad words found\n";

open BAD, 'badwords.txt';

foreach my $line (<BAD>) {
chomp $line;
next unless $line eq $input_message;
$response = "Bad Words Found\n";
last;
}

print $response;