Jump to content

Compare code working twice


manalnor

Recommended Posts

Hello dear friends,

 

okay here is the story :)

 

i've 2 incoming entries

 

$name = "Manal Nor";
$comment = "Hello lovely world";

 

and i've stored into database table some bad words to be banned

 

my_table (id,word)

wsX2s.png

 

My objective

Is to compare if $name and/or $comment have any of the banned words in my_table

 

 

I can apply for $name only , i mean i can compare $name and know if it have any banned words or not using the following code

 

$name = "Manal Nor"; // Example .. no bad words

$sql    = "SELECT * FROM my_table";
$result = mysql_query($sql);

$nameArray = explode(" ", $name);
$countname = count($nameArray);
$checkname = 0;

while ($row = mysql_fetch_assoc($result)) {

for ($i == 0; $i < $countname; $i++) {

if (strcasecmp($nameArray[$i], $row['word'])) {
$checkname = 1;
}

}

}

if ($checkname == 1) {
echo "banned";
exit;
}else {
echo "passed";
}

 

it works perfect  ::)

 

but now the question how to apply it like cheese burger i mean for both $name and $comment so that i can use

 

:'( any help please

Link to comment
Share on other sites

Is there enough bad words that you think it would make more sense to store them in a database? I would store them in an array and compare each word against the array.

 

$bad_words = array('shit','bitch','witch');
$name = "Manal Nor";
$comment = "Hello lovely world";
$full_string = strtolower($name . " " . $comment);
$full_string_arr = explode(" ", $full_string);
$check = 0;
foreach($full_string_arr as $word)
{
    if(in_array($word, $bad_words))
    {
        $check = 1;
        break;   
    }
}
if($check == 1)
{
//bad word contained in string
}

Link to comment
Share on other sites

@AyKay47

 

in fact i want to do it to prevent a large list of certain banned medicine posted by some spam bots.

 

anyway i know it was very complex and hard to think how to apply it for both

but thanks for PHP manual , i've found good way to even apply it for more and 2 entries  ;D

 

using

array_merge

so it would be

$nameArray = array_merge(explode(" ", $name), explode(" ", $comment));

 

and go on with the rest of the code :) that is it.

 

Thanks a lot for your time AyKay47

and your code is effective but i love to store everything in database  :D

 

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.