I have a filter function which is supposed to check two “fields” in an array, these are cold name and description.
I also have words stored in a database which the filter function should accept certain words (true) and decline certain words (false).
Example:
Every post coming in containing the word car should be accepted.
In the database I have stored following words which is accepted:
car (obviously)
Not accepted:
truck
traktor
buss
So I have a function that is supposed to go through the two fields (name and description) in the array match them to the words stored in the database and look if the fields contains the accepted word or any of the not accepted words.
My function looks like this
$arr = incoming array with the fields name and description.
$a[‘value’] = If it is 0 the word is not accepted if it is 1 the word are accepted.
$a[‘filter’] = The word from the database.
function checkFilter($arr)
{
$name = strtolower($arr['name']);
$description = strtolower($arr['description']);
$dbh = $this->connect();
$rs = $dbh->prepare("SELECT * FROM filter");
$rs->execute();
$all = $rs->fetchAll();
foreach($all as $a)
{
if($a['value'] == 0 && strstr($name, strtolower($a['filter']), true))
{
return false;
break;
}
if($a['value'] == 0 && strstr($description, strtolower($a['filter']), true))
{
return false;
break;
}
if($a['value'] == 1 && strstr($name, strtolower($a['filter']), true))
{
return true;
break;
}
if($a['value'] == 1 && strstr($description, strtolower($a['filter']), true))
{
return true;
break;
}
}
return false;
}
As you se my thought here is, if any of the not acceptable words are showing up I want to return false from the function and break. This because it doesn’t matter if the accepted word is in the name or description if the not accepted word is already there.
After the foreach I have a return false because if it can’t find the accepted word (eg car) it should be filtred out.
My problem is that this doesn’t seems to work. It doesn’t seems to break the function after it hase come into an if statement.
How should I solve this?
Try this: