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]
Link to comment
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]

Link to comment
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]
Link to comment
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"; }

?>
Link to comment
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"; }

?>
Link to comment
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.
Link to comment
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?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.