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
https://forums.phpfreaks.com/topic/257825-compare-code-working-twice/
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
}

@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

 

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.