Jump to content

Archived

This topic is now archived and is closed to further replies.

redarrow

stop words in database with array valadation

Recommended Posts

Advance thank you.

The varable that holds the information is $message.

how can i valadate what words are in the message verable like this.

example:

[code]

$words=array("@" , "{ ","   ,    "$"  ,   " ("  ,   " } "  ,   "+"  ,  "," )

if($words==$message) {

echo " sorry charecter used not allowed";

}

[/code]

Share this post


Link to post
Share on other sites

This will replace the words not wanted but i want the same format but halting and echo a message cheers.


example:

[code]

$mess=array("@","*","$","'","{","}",";","<br>","mysql","connect","</br>","password","money","scam",".","com"
,"net","uk");

$message=str_replace($mess,"xxx" ,$message);

echo $message;


[/code]

Share this post


Link to post
Share on other sites
foreach($mess as $val)
{

$string = $mesage;
if (eregi('$val', $string)) {
echo "Sorry, $val is not allowed.";
}

}

Share this post


Link to post
Share on other sites

I got this so far but dosent work.

The varable is comming from the form so far before the database insert cheers.

[code]

$mess=array("@","*","$","'","{","}",";","<br>","mysql","connect","</br>","password","money","scam",".","com"
,"net","uk");

foreach($mess as $val)
{

$string = $message;
if (eregi('$val', $string)) {
echo "Sorry, $val is not allowed.";
}

}
[/code]

Share this post


Link to post
Share on other sites

my error


[code]

Sorry, @ is not allowed.
Warning: ereg(): REG_BADRPT in C:\Program Files\Apache Group\Apache2\htdocs\collage\bands\mail\mail.php on line 43
Sorry, $ is not allowed.Sorry, . is not allowed
[/code]

Share this post


Link to post
Share on other sites
<?php
// only problem with this is that the word "coming" (for example) contains com, which is not allowed in your list.
// to get around this, put a space either side of com, filter for .com instead.

$message = "Your message here probably from a from on the previous page";
$mess=array("@", "*", "$", "'", "{", "}", "<br>", "mysql", "connect", "</br>", "password", "money", "scam", ".", "com", "net", "uk");

foreach($mess as $val)
{
$invalid = strpos($message, $val);
if($invalid===FALSE) { $count++; }

}

$array_count = count($mess, COUNT_RECURSIVE);
if($count==$array_count) { echo "Contains no illegal characters";}
else { echo "Your message contains illegal words / characters"; }

?>

Share this post


Link to post
Share on other sites
Can some one brake this down to a babys level please lol cheers mate!

wisewood thats agrate bit of programming cheers please exsplain the code i only understand 50% of it beg you.

thanks redarrow.

Share this post


Link to post
Share on other sites
// Your message that you want to check for illegal characters/words.
// $_POST[message] for example

$message = "Your message here probably from a from on the previous page";

// Your array of things that you do not want added to the database
$mess=array("@", "*", "$", "'", "{", "}", "<br>", "mysql", "connect", "</br>", "password", "money", "scam", ".", "com", "net", "uk");

// For each of the values in the $mess array, set it as $val and check if it exists in the $message variable.
// If it does not exist, add 1 to the $count variable.
foreach($mess as $val)
{
$invalid = strpos($message, $val);
if($invalid===FALSE) { $count++; }

}

// Count the number of words/characters that are in the array
$array_count = count($mess, COUNT_RECURSIVE);

// If the $count variable is the same as the $array_count variable, there are no illegal characters
if($count==$array_count) { echo "Contains no illegal characters";}

// if the $count and $array_count variables do not match, there must be at least one illegal character in the $message variable.
else { echo "Your message contains illegal words / characters"; }

?>

Share this post


Link to post
Share on other sites
can you kindly exsplain this in easy terms cheers then solved thank you so much.
COUNT_RECURSIVE

Share this post


Link to post
Share on other sites
COUNT_RECURSIVE is an optional component of the count() function.

if you were to use count($mess) and mess contained two arrays, the result would be 2.

However, by using count($mess, COUNT_RECURSIVE) you end up with it counting the contents of the $mess array, as well as the contents of any arrays stored within mess.

To be honest, in this case, i dont think its required, but i used it anyway.

Share this post


Link to post
Share on other sites
Maybe everyone has gone a little offtrack, all you need is a simple function called "in_array"
ie:

[code]
$message = "Hello ()";
$words=array("@","{","(","$","(","}","+");
for($i=0;$i<strlen($message);$i++) {
   if(in_array($message[$i],$words)) {
      echo "$message.<br>sorry charecter used not allowed. <b>".$message[$i]." at position $i</b>";
   }
}
[/code]

Maybe thhat will help?

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.