PHP & Text Matching

Discuss Programming
Post Reply
agent007
administrator
administrator
Posts: 254
Joined: Wed Feb 12, 2003 11:26 pm

PHP & Text Matching

Post by agent007 » Mon Feb 02, 2004 1:09 am

hi guys,

I'm using PHP to do some form validation..Basically, to block words related to porn and violence. Only problem is, if the word is
changed to CAPS, for example, 'Rude' or 'rUde' or 'RUDE', then the user is able to make the submission. The original word to be banned was 'rude'..So, is there a way to make the code check for these conditions. Right now, the code looks like this:

$list="SELECT * from banned";
$list2=mysql_query($list) or die("Could not get list");
while($list3=mysql_fetch_array($list2))
{
if(substr_count($url,$list3[word])>0)
{
$illegal++;
}
if(substr_count($title,$list3[word])>0)
{
$illegal++;
}
if(substr_count($description,$list3[word])>0)
{
$illegal++;
}
}
if($illegal>0)
{
print "<br><h2>You have entered a blacklisted/banned word....</h2>";
}

TIA

Tux
guru
guru
Posts: 689
Joined: Wed Jan 08, 2003 10:40 am

Post by Tux » Mon Feb 02, 2004 4:52 am

Will PHP let you use a regexp? Im sure it will, try that :)

agent007
administrator
administrator
Posts: 254
Joined: Wed Feb 12, 2003 11:26 pm

Post by agent007 » Mon Feb 02, 2004 6:20 am

I just thought of an idea. Would it be possible so that it allows only lowercase characters to be inputed? That would really make things simple...

thanks..

agent007
administrator
administrator
Posts: 254
Joined: Wed Feb 12, 2003 11:26 pm

Post by agent007 » Mon Feb 02, 2004 11:54 am

well, I was able to find a solution!
if (eregi("^[A-Z]", $title)) {
print "</h2>Only lowercase characters are allowed!</h2>";
return FALSE;
PHP rocks!!

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main » Mon Feb 02, 2004 12:33 pm

That will ignore case when comparing which is what you wanted. You can also do a similar thing with the strtolower() function, as well as actually converting the string to lower case if you want:

Check:

Code: Select all

if (strtolower($str) == "lowstring") {
Convert string to lower case:

Code: Select all

$lowstr = strtolower($str);

agent007
administrator
administrator
Posts: 254
Joined: Wed Feb 12, 2003 11:26 pm

Post by agent007 » Tue Feb 03, 2004 5:49 am

Void,

What did u mean by ignore case? By using eregi, I'm able to block all attempts when the user tries to input CAPS in the title field..It does not ignore the CAPS since its case-sensitive. IF everything is in lowercase, the PHP then compares the text with the list of black-listed words from sql. So, this way, I dump all the black-listed words in the db in lowecase. Before, it was possible for someone to use 'cRap' and SPAM the system.

I dont see any workaround now, since I'm forcing all input to be lowecase...then again, could there be a workaround?

Void Main wrote:That will ignore case when comparing which is what you wanted.

agent007
administrator
administrator
Posts: 254
Joined: Wed Feb 12, 2003 11:26 pm

Post by agent007 » Tue Feb 03, 2004 8:52 pm

i've come across a funny thing...The following code:
if (ereg("[A-ZÀ-ÖØ-öø-ÿ`~!@&*()=+{}:;,.<>?|1-9]", $title))
print "</h2>Only lowercase characters are allowed!</h2>";
return FALSE;
does not allow strings like aGent

but, this code:
if (ereg("[a-z]", $title))
{
}
else
{
print "</h2>Only lowercase characters are allowed!</h2>";
return FALSE;
}
allows strings like aGent but not AGENT..I feel this is a better way to do form validation, since *all* characters other than a-z would be blocked...atleast thats the way its supposed to work.

any ideas?
TIA

ZiaTioN
administrator
administrator
Posts: 460
Joined: Tue Apr 08, 2003 3:28 pm
Contact:

Post by ZiaTioN » Tue Feb 03, 2004 9:12 pm

I would have to say that Voids suggestion would be your best bet for you and your users. That way your users are still allowed to use caps if they want but each string will be converted to lowercase when parsed. You do not have to actually alter the users post but just read in the post and convert to lowercase to compare and if no banned words are found then allow the post to be posted or what not.

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main » Tue Feb 03, 2004 9:25 pm

I usually just use preg_match when comparing two things and ignore case:

Code: Select all

if (preg_match("/$word/i",$title)) {
   echo "You've entered a naughty word, shame on you!\n";
}
The "i" means "ignore case". Also if you don't need to keep the case of anything input then convert everything to lower case using the strtolower() as mentioned. But I could very well not be understanding what you are getting at.

Post Reply