Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing a Variable 1

Status
Not open for further replies.

nochoice

Programmer
Jun 17, 2003
72
0
0
CA
Hey Everybody,

I have my users enter some information into a form and submit it.

Then I do the standard checking of the form to make sure all entries were filled in correctly, if they were, the information is posted.

What I'm worried about is swearing and other such bad language. I would like to check through the variable for these things. For example,

the user enters their name where:

$name = "f......ing Jerk"

instead of just $name=~ s/f.....ing//;

I would like to give some feedback to the user for swearing so I want to detect the offensive language.

Any suggestions?

I'm basically lost here.

Thanks in advance,
Mike

 
Well I think you would have to create an array containing all the words that you would consider "offensive". Then you would have do a loop and cross reference the array to the field to see if there are any matches, if so output the appropriate message, if not, continue as normal.

I think that would be your basic logic of your program, which isn't hard at all. The only problem is writting down all the different "offensive" words out there. Maybe you could do a search for a swearword database or something.

Best of luck.

If you have an problem with the coding, just give us a shout.

Sean.
 
Ok, both look like good options.

I am attempting to implement Regexp::Common::profanity but would also like to try the array deal, I see it as being a little more flexible - although tedious.

If I had a variable $name and it's value was "MikPPPe JohnPPPston":

$name = "MikPPPe JohnPPPston";

how would I search the variable to find the PPP and return true if it was found. I'm looking for an actual code snippet if possible.

Thanks,
Michael
 
I think this might do:

if ($string =~ m/StringOrRegExpr/) {
print 'match';
} else {
print 'no match';
}

Then you could replace StringOrRegExpr with $array[12] for eg.

For your prog you could do this:

$len = scalar(@arrayname); #Get length of array
$match = "NO";

for(int i = 0; i < $len; i++){
if ($string =~ m/$arrayname/) {
$match = &quot;YES&quot;;
}
}

if($match eq &quot;YES&quot;){
print &quot;You cannot enter a swear word...etc&quot;;
}
else{
continue.
}

Hope this helps out in some way.


Sean.
psa.gif
 
woops, it did some editing up there, use this instead:

Code:
@swearwords=(&quot;f**k&quot;,&quot;s**t&quot;);

$len = scalar(@swearwords);  #Get length of array
$match = &quot;NO&quot;;

for(int p = 0; p < $len; p++){
     if ($string =~ m/$arrayname[p]/) {
         $match = &quot;YES&quot;;
     }
}

if($match eq &quot;YES&quot;){
     print &quot;You cannot enter a swear word...etc&quot;;
}
else{
     continue.
}


Sean.
psa.gif
 
Damn, another error!

$arrayname[p] in the if statement must be $swearwords[p]


Sean.
psa.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top