Old 12-05-2007, 23:40   #1 (permalink)
pgo
i'm done, son
 
Join Date: Jan 2005
Posts: 12,262
banned words script

I'm rebuilding the directory with my own backend. I'm implementing a "banned words" feature, where I can ban certain words (offensive, spam, etc). Here's the code that's checking them (it checks the title and the description for banned words).

My fear is that it's going to take a lot of processing time, so I've tried to make it do as little work as possible - when it finds something, it stops and if it finds something in the $title, it doesn't check the $descr.

PHP Code:
// Query to start checking for banned words.
$query $dbconn->query("select string from banned where type='word'");
    
if (
$query->num_rows 0) {
  
// Initialize variables if banned words are found in the database.
  
$i 0;
  
$found_banned 'no';

  
// Pull banned words from database into a numeric array.
  
while ($row $query->fetch_assoc()) {
    
$bans[$i] = strtolower($row['string']);
    
$i++;
  }

  
// Create word-by-word arrays for each input to be checked.
  
$title explode(' 'strtolower($_POST['link-title']));
  
$descr explode(' 'strtolower($_POST['link-desc']));

  
// Check $title first in case we can avoid processing time by not checking $descr.
  
foreach ($title as $word) {
    for (
$i 0$i count($bans); $i++) {
      
// Cycle through the banned words array and test each element in that array against the current word from the $title array.
      
if ($word == $bans[$i]) {
        
$found_banned 'yes';
        break;
      }
    }
  }

  
// If the $title array contained no banned words, move on and test the $descr array.
  
if ($found_banned == 'no') {
    foreach (
$descr as $word) {
      for (
$i 0$i count($bans); $i++) {
        
// Cycle through the banned words array and test each element in that array against the current word from the $descr array.
        
if ($word == $bans[$i]) {
          
$found_banned 'yes';
          break;
        }
      }
    }
  }

I'm using "yes" and "no" as my tests because I couldn't get boolean values to work properly. There must be something with them I'm not getting.

Seeing as I still consider myself a PHP/MySQL beginner (a new book on OOP is on its way via UPS!), even though it works, is there a better way to do this?
  Reply With Quote
Old 13-05-2007, 01:45   #2 (permalink)
Cborrow
I like code.
 
Join Date: Dec 2004
Location: Chesapeake, VA
Posts: 200
Send a message via AIM to Cborrow
How about just using regular expressions and preg_replace to replace the words.

An example of a possible solution.

PHP Code:
$bad_words = array("(no)");
$replace_words = array("yes");
$content "This is a string with no in it";

$content preg_replace($bad_words$replace_words$content); 

If you are going to try to replace words with just blank space preg_replace wont work you will have to use str_replace or similar.
  Reply With Quote
Old 13-05-2007, 02:26   #3 (permalink)
gk
geek
 
gk's Avatar
 
Join Date: Oct 2006
Location: *.everywhere
Posts: 204
Send a message via ICQ to gk Send a message via AIM to gk Send a message via MSN to gk Send a message via Yahoo to gk
Why go through all the loops? can't you just do something like

PHP Code:
$words=explode(' ',$string);
$sql="SELECT `string` from `banned` where `type`='word' AND `string` IN (";
foreach(
$words as $word){
if(
strlen($word)>1){
$sql.="'$word',";
}
}

$sql=rtrim($sql,',').')';
$query $dbconn->query("select string from banned where type='word'");
    
if (
$count=$query->num_rows 0) {
echo 
"You've been a bay boy. You said $count bad words\n";

  while (
$row $query->fetch_assoc()) {
    echo 
"You said \"{$row['string']}\", that's bad Umkay.\n";
  }



It should work... you could add "limit 1" to the SQL statement if you want a simple good/bad response.

if you want to save processing power you coudl do some sort of comparison to the number of words n your title/description to the number of banned words in your database then check one or the other depending on who has less words.

Cborrow's method would be fastest if your banned words list is short. Just replace preg_replace with preg_match and it's good.
__________________
Quote:
"Why reinvent the wheel"? Simply because it's not round enough and I don't like the treads.
  Reply With Quote
Old 13-05-2007, 12:16   #4 (permalink)
pgo
i'm done, son
 
Join Date: Jan 2005
Posts: 12,262
I'd use the preg_replace method if I were replacing them. As it stands, this is just another error check on a form submission.

Thanks for the suggestion, gk. I'll see what I can do with a query.

Last edited by pgo : 13-05-2007 at 12:38.
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8