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?