manalnor Posted February 26, 2012 Share Posted February 26, 2012 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) 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 Quote Link to comment https://forums.phpfreaks.com/topic/257825-compare-code-working-twice/ Share on other sites More sharing options...
AyKay47 Posted February 26, 2012 Share Posted February 26, 2012 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 } Quote Link to comment https://forums.phpfreaks.com/topic/257825-compare-code-working-twice/#findComment-1321467 Share on other sites More sharing options...
manalnor Posted February 26, 2012 Author Share Posted February 26, 2012 @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 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 Quote Link to comment https://forums.phpfreaks.com/topic/257825-compare-code-working-twice/#findComment-1321468 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.