Jump to content

stop words in database with array valadation


redarrow

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]

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]


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]
<?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"; }

?>
// 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"; }

?>
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.
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?

Archived

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

×
×
  • Create New...

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.